1 Description of current data

ADT <- LC_YA_AVDT %>%
    filter(task == 'aud') %>%
    filter(!is.na(resp1)) %>%
    filter(sub != '44')

VDT <- LC_YA_AVDT %>%
    filter(task == 'vis') %>%
    filter(!is.na(resp1))

CDT <- LC_YA_CDT %>%
    filter(task == 'cdt') %>%
    filter(!is.na(resp1))

MST <- LC_YA_MST %>%
    filter(task == 'MSTtest') %>%
    filter(!is.na(resp1)) %>%
    filter(sub != 21)

# Function to calculate total trials per subject
calculate_total_trials <- function(data) {
    data %>%
        group_by(sub) %>%
        summarise(total_trials = n())
}

# Calculate total trials for ADT and VDT and CDT and MST
total_trials_ADT <- calculate_total_trials(ADT)
total_trials_VDT <- calculate_total_trials(VDT)
total_trials_CDT <- calculate_total_trials(CDT)
total_trials_MST <- calculate_total_trials(MST)


# Combine the data frames
combined_data <- bind_rows(
    mutate(total_trials_ADT, task = "ADT"),
    mutate(total_trials_VDT, task = "VDT"),
    mutate(total_trials_CDT, task = "CDT"),
    mutate(total_trials_MST, task = "MST")
)

# Create a facetted bar plot
facetted_bar_plot <- ggplot(combined_data, aes(x = sub, y = total_trials, fill = task)) +
    geom_bar(stat = "identity", position = "dodge", alpha = 0.7) +
    labs(title = "Total Number of Trials per Subject",
         x = "Subject",
         y = "Total Trials",
         fill = "Task") +
    facet_wrap(~task, scales = "free_y", ncol = 1) +
    theme_minimal()
ggplotly(facetted_bar_plot)

2 ADT

2.1 Prportion of Responding ‘Different’

# calculate subject mean
ADT_acc_ws <- ADT %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1_diff, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
ADT_acc_ws <- ADT_acc_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

ADT_acc_ws <- ADT_acc_ws %>%
  mutate(acc_diff = sub_mean- grand_mean) 

# Calculate mean accuracy and standard error for 'Different'
Diff_prop_ADT <- ADT_acc_ws %>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(diff=mean(diff_sub), diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate the proportion of 'Same'
Same_prop_ADT <- ADT_acc_ws %>%
    group_by(sub, stimLev) %>%
    summarise(same_sub = 1-mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(not_diff=mean(same_sub), not_diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

# Combine both datasets for plotting
combined_ADT <- bind_rows(
  mutate(Diff_prop_ADT, condition = "Different", prop = diff),
  mutate(Same_prop_ADT, condition = "Same", prop = not_diff)
)

# Specify the desired order of levels for 'stimLev'
desired_order <- c("0", "4", "8", "32", "128")

# Convert 'stimLev' to factor with the desired order
combined_ADT$stimLev <- factor(combined_ADT$stimLev, levels = desired_order)

# Plotting with the modified 'stimLev'
ggplot(combined_ADT, aes(x = stimLev, y = prop, group = condition, color = condition)) +
  geom_point(size = 2) +
  geom_line(size = 1.2) +
  geom_linerange(mapping = aes(ymin = prop - ifelse(condition == "Different", diff_se, not_diff_se), 
                                ymax = prop + ifelse(condition == "Different", diff_se, not_diff_se))) +
  ylim(0, 1) +
  ggtitle("Proportion of Responding 'Different' and 'Same' - ADT'") +
  xlab("Oddball Level (Hz)") +
  ylab("Proportion") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("#BC3C29FF", "#0072B5FF")) +
  guides(color = guide_legend(title = "condition"))

2.1.1 Handgrip manipulation

2.1.1.1 Binary

# calculate subject mean
ADT_acc_ws <- ADT %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1_diff, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
ADT_acc_ws <- ADT_acc_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

ADT_acc_ws <- ADT_acc_ws %>%
  mutate(acc_diff = sub_mean- grand_mean) 

# Calculate mean accuracy and standard error for 'Different'
Diff_prop_high_grip_ADT <- ADT_acc_ws %>%
    filter(isStrength==1)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(diff=mean(diff_sub), diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

Diff_prop_low_grip_ADT <- ADT_acc_ws %>%
    filter(isStrength==0)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(diff=mean(diff_sub), diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate the proportion of 'Same'
Same_prop_high_grip_ADT <- ADT_acc_ws %>%
    filter(isStrength==1) %>%
    group_by(sub, stimLev) %>%
    summarise(same_sub = 1-mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(not_diff=mean(same_sub), not_diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

Same_prop_low_grip_ADT <- ADT_acc_ws %>%
    filter(isStrength==0) %>%
    group_by(sub, stimLev) %>%
    summarise(same_sub = 1-mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(not_diff=mean(same_sub), not_diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

# Combine both datasets for plotting
combined_grip_ADT <- bind_rows(
  mutate(Diff_prop_high_grip_ADT, condition = "Different - High Grip", prop = diff),
  mutate(Diff_prop_low_grip_ADT, condition = "Different - Low Grip", prop = diff),
  mutate(Same_prop_high_grip_ADT, condition = "Same - High Grip", prop = not_diff),
  mutate(Same_prop_low_grip_ADT, condition = "Same - Low Grip", prop = not_diff)
)

# Specify the desired order of levels for 'stimLev'
desired_order <- c("0", "4", "8", "32", "128")

# Convert 'stimLev' to factor with the desired order
combined_grip_ADT$stimLev <- factor(combined_grip_ADT$stimLev, levels = desired_order)

# Plotting
ggplot(combined_grip_ADT, aes(x = stimLev, y = prop, group = condition, color = condition)) +
  geom_point( size = 2) +
  geom_line(size = 1.2) +
  geom_linerange(mapping = aes(ymin = prop - ifelse(condition == "Different - High Grip" | condition == "Different - Low Grip", diff_se, not_diff_se), 
                               ymax = prop + ifelse(condition == "Different - High Grip" | condition == "Different - Low Grip", diff_se, not_diff_se)), show.legend = FALSE) +
  ylim(0, 1) +
  ggtitle("Proportion of Responding 'Different' and 'Same' - ADT'") +
  xlab("Oddball Level (Hz)") +
  ylab("Proportion") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("Different - High Grip" = "#BC3C29FF", "Different - Low Grip" = "#ea9999", "Same - High Grip" = "#0072B5FF", "Same - Low Grip" = "#CCE6FF")) +
  scale_linetype_manual(values = c("Different - High Grip" = "solid", "Different - Low Grip" = "solid", "Same - High Grip" = "solid", "Same - Low Grip" = "solid")) +
  guides(color = guide_legend(title = "Response Type", ncol = 1), linetype = "none")

2.1.1.2 Continuous

library(ggplot2)

lower_limit <- 0  # Lower limit
upper_limit <- 0.6  # Upper limit

# Filter the data based on the auc_rel_mvc limits
ADT_acc_ws <- ADT_acc_ws %>%
    filter(auc_rel_mvc <= upper_limit)
ADT_acc_ws <- ADT_acc_ws %>%
    filter(auc_rel_mvc <= upper_limit)

# Specify the desired order of levels for 'stimLev'
desired_order <- c("0", "4", "8", "32", "128")

# Convert 'stimLev' to factor with the desired order
ADT_acc_ws$stimLev <- factor(ADT_acc_ws$stimLev, levels = desired_order)


# Define the plot aesthetics
ggplot(ADT_acc_ws, aes(x = stimLev, y = auc_rel_mvc, color = factor(resp1_diff))) +
  
  # Add points for scatter plot
  #geom_point(alpha = 0.7) +
  
  # Set plot title and axis labels
  ggtitle("Relationship between Stimulus Level, Grip Force, and Same or Different - ADT") +
  xlab("Oddball Level (Hz)") +
  ylab("Relative Grip") +
  geom_jitter(alpha = 0.4, width = 0.2, height = 0.05) +

 # Customize legend
  scale_color_manual(values = c("#0072B5FF", "#BC3C29FF"), labels = c("Same (0)", "Different (1)")) +
  scale_size_manual(values = c(3, 6), labels = c("Same (0)", "Different (1)")) +
  guides(color = guide_legend(title = "Response Type"),
         size = guide_legend(title = "Response Type"))

2.1.1.3 Low vs High handgrip for each subject across oddball levels

ADT$gf_trPer <- as.factor(ADT$gf_trPer)
ADT$gf_trPer <- recode_factor(ADT$gf_trPer, '0.05' = "Low", '0.4' = "High")
# Calculate individual subject responses
subject_responses_ADT_2 <- ADT %>%
  group_by(sub, stimLev, gf_trPer) %>%
  summarise(response = mean(resp1_diff, na.rm = TRUE)) %>%
  arrange(gf_trPer) %>%
  ungroup()

# Replace 'sub' with 'gf_trPer' for X-axis labels
subject_responses_ADT_2$gf_trPer <- factor(subject_responses_ADT_2$gf_trPer, levels = c("Low", "High"))

# Plotting
p10_ADT <- ggplot(subject_responses_ADT_2, aes(x = gf_trPer, y = response, color=gf_trPer, group = sub, text =   as.character(sub))) +
  geom_line(color = "gray") +
  geom_point(size = 2) +
  ggtitle("Proportion of Responding 'Different'") +
  scale_color_manual(values = c("#0072B5FF", "#BC3C29FF")) +
  xlab("Grip Level") +
  ylab("P('Different')") +
  facet_wrap(~stimLev, scales = "free_x", ncol = length(unique(subject_responses_ADT_2$stimLev)))+
  guides(color = guide_legend(title = "Grip Level"))

ggplotly(p10_ADT, tooltip = "text")

2.2 Reaction time

2.2.1 Using Median with within-subject SEM

# calculate subject mean
ADT_RT_ws <- ADT %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1RT, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calcualte the grand mean
ADT_RT_ws <- ADT_RT_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

ADT_RT_ws <- ADT_RT_ws %>%
  mutate(RT_diff = sub_mean- grand_mean) 


# Calculate median RT for different responses
ADT_diff_RT_ws <- ADT_RT_ws %>%
  filter(resp1_diff == 1) %>%
  group_by(stimLev,sub) %>%
  summarise(diff_sub= median(resp1RT), RT_diff) %>%
  ungroup() %>%
  group_by(stimLev) %>%
  summarise(medRT_diff = median(diff_sub),
            medRT_diff_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
  distinct()

# Calculate median RT for same responses
ADT_same_RT_ws <- ADT_RT_ws %>%
  filter(resp1_diff == 0) %>%
  group_by(stimLev,sub) %>%
  summarise(diff_sub= median(resp1RT), RT_diff) %>%
  ungroup() %>%
  group_by(stimLev) %>%
  summarise(medRT_same = median(diff_sub),
            medRT_same_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
  distinct()

# Combine both datasets for plotting
combined_RT_ADT <- bind_rows(
  mutate(ADT_diff_RT_ws, condition = "Different"),
  mutate(ADT_same_RT_ws, condition = "Same")
)


# Specify the desired order of levels for 'stimLev'
desired_order <- c("0", "4", "8", "32", "128")

# Convert 'stimLev' to factor with the desired order
combined_RT_ADT$stimLev <- factor(combined_RT_ADT$stimLev, levels = desired_order)

#Sort the combined dataset by stimLev within each condition
combined_RT_ADT <- combined_RT_ADT %>%
  arrange(condition, stimLev)

# Plotting
ggplot(combined_RT_ADT, aes(x = stimLev, group = condition, color = condition, linetype= condition)) +
  geom_point(aes(y = ifelse(condition == "Different", medRT_diff , medRT_same)), size = 2) +
  geom_line(aes(y = ifelse(condition == "Different", medRT_diff, medRT_same)), size = 1.2) +
  ylim(min(c(combined_RT_ADT$medRT_diff, combined_RT_ADT$medRT_same)), max(c(combined_RT_ADT$medRT_diff, combined_RT_ADT$medRT_same))) +
  ggtitle("Median of Reaction Time for Same and Different Responses - ADT") +
  geom_linerange(mapping = aes(ymin = medRT_diff - medRT_diff_se, ymax = medRT_diff + medRT_diff_se, linetype = condition), data = filter(combined_RT_ADT, condition == "Different")) +
  geom_linerange(mapping = aes(ymin = medRT_same - medRT_same_se, ymax = medRT_same + medRT_same_se, linetype = condition), data = filter(combined_RT_ADT, condition == "Same")) +
  labs(caption = "Error bars represent within-subject SEM") +
  xlab("Oddball Level (Hz)") +
  ylab("Reaction Time (S)") +
  scale_color_manual(values = c( "#BC3C29FF", "#0072B5FF")) +
  scale_linetype_manual(values = c("Different" = "solid", "Same" = "solid")) 

2.2.2 Handgrip manipulation

2.2.2.1 Binary

# calculate subject mean
ADT_RT_ws <- ADT %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1RT, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calcualte the grand mean
ADT_RT_ws <- ADT_RT_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

ADT_RT_ws <- ADT_RT_ws %>%
  mutate(RT_diff = sub_mean- grand_mean) 

# Calculate mean RT and standard error for  High 'Different'
RT_diff_high_grip_ADT <- ADT_RT_ws %>%
    filter(resp1_diff == 1) %>%
    filter(isStrength==1)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_high_diff=median(diff_sub), medRT_high_diff_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate mean RT and standard error for  Low 'Different'
RT_diff_low_grip_ADT <- ADT_RT_ws %>%
    filter(resp1_diff == 1) %>%
    filter(isStrength==0)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_low_diff=median(diff_sub), medRT_low_diff_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()


# Calculate mean RT and standard error for  High 'Same'
RT_same_high_grip_ADT <- ADT_RT_ws %>%
    filter(resp1_diff == 0) %>%
    filter(isStrength==1)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_high_same=median(diff_sub), medRT_high_same_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate mean RT and standard error for  Low 'Same'
RT_same_low_grip_ADT <- ADT_RT_ws %>%
    filter(resp1_diff == 0) %>%
    filter(isStrength==0)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_low_same=median(diff_sub), medRT_low_same_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()


# Combine both datasets for plotting
combined_RT_grip_ADT <- bind_rows(
  mutate(RT_diff_high_grip_ADT, condition = "Different - High Grip"),
  mutate(RT_diff_low_grip_ADT, condition = "Different - Low Grip"),
  mutate(RT_same_high_grip_ADT, condition = "Same - High Grip"),
  mutate(RT_same_low_grip_ADT, condition = "Same - Low Grip")
)

# Specify the desired order of levels for 'stimLev'
desired_order <- c("0", "4", "8", "32", "128")

# Convert 'stimLev' to factor with the desired order
combined_RT_grip_ADT$stimLev <- factor(combined_RT_grip_ADT$stimLev, levels = desired_order)


# Plotting
ggplot(data = combined_RT_grip_ADT) +
  geom_point(aes(x = stimLev, y = medRT_high_diff, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_high_diff, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_high_diff - medRT_high_diff_se, 
                     ymax = medRT_high_diff + medRT_high_diff_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_low_diff, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_low_diff, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_low_diff - medRT_low_diff_se, 
                     ymax = medRT_low_diff + medRT_low_diff_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_high_same, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_high_same, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_high_same - medRT_high_same_se, 
                     ymax = medRT_high_same + medRT_high_same_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_low_same, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_low_same, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_low_same - medRT_low_same_se, 
                     ymax = medRT_low_same + medRT_low_same_se, color = condition), 
                 linetype = "solid") +
  
  ggtitle("Median of Reaction Time for Same and Different Responses - ADT") +
  xlab("Oddball Level (Hz)") +
  ylab("Reaction Time") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("#BC3C29FF", "#ea9999", "#0072B5FF", "#CCE6FF"),
                      name = "Condition") 

2.3 Confidence

2.3.1 Within-subject SEM

# calculate subject mean
ADT_conf_ws <- ADT %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp2, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
ADT_conf_ws <- ADT_conf_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

ADT_conf_ws <- ADT_conf_ws %>%
  mutate(conf_diff = sub_mean- grand_mean) 

diff_average_confidence_ADT <- ADT_conf_ws %>%
        filter(resp1_diff== 1) %>%
        group_by(stimLev, sub) %>%
        summarise(diff_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(diff_conf = mean(diff_prop, na.rm = T), 
                  diff_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()

same_average_confidence_ADT <- ADT_conf_ws %>%
        filter(resp1_diff== 0) %>%
        group_by(stimLev, sub) %>%
        summarise(same_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(same_conf = mean(same_prop), 
                  same_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()

  combined_prop_ADT <- bind_rows(
        mutate(diff_average_confidence_ADT, condition = "Different"),
        mutate(same_average_confidence_ADT, condition = "Same")
    )
  
    # Specify the desired order of levels for 'stimLev'
    desired_order <- c("0", "4", "8", "32", "128")
    
    # Convert 'stimLev' to factor with the desired order
    combined_prop_ADT$stimLev <- factor(combined_prop_ADT$stimLev, levels = desired_order)
    
    # Sort the combined dataset by stimLev within each condition
    combined_prop_ADT <- combined_prop_ADT %>%
        arrange(condition, stimLev)
    
    # Plotting
    ggplot(combined_prop_ADT, aes(x = stimLev, group = condition, color = condition)) +
        geom_point(aes(y = ifelse(condition == "Different", diff_conf, same_conf)), size = 2) +
    geom_line(aes(y = ifelse(condition == "Different", diff_conf, same_conf)), size = 1.2) +
        ylim(2, 4) +
        ggtitle("Average of Confidence Response - ADT") +
        geom_linerange(mapping = aes(ymin = diff_conf - diff_conf_se, ymax = diff_conf + diff_conf_se, linetype = condition), data = filter(combined_prop_ADT, condition == "Different")) +
    geom_linerange(mapping = aes(ymin = same_conf - same_conf_se, ymax = same_conf + same_conf_se, linetype = condition), data = filter(combined_prop_ADT, condition == "Same")) +
        labs(caption = "Error bars represent within-subject SEM") +
        xlab("Oddball Level") +
        ylab("Confidence Rating") +
        scale_color_manual(values = c("Different" = "#BC3C29FF",
                                      "Same" = "#0072B5FF"
                                      ),
                           labels = c("Different",
                                      "Same"
                                      )) +
        scale_linetype_manual(values = c("Different" = "solid", "Same" = "solid"))

2.3.2 Handgrip manipulation

2.3.2.1 Binary

# calculate subject mean
ADT_conf_ws <- ADT %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp2, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
ADT_conf_ws <- ADT_conf_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

ADT_conf_ws <- ADT_conf_ws %>%
  mutate(conf_diff = sub_mean- grand_mean) 

diff_average_high_confidence_ADT <- ADT_conf_ws %>%
        filter(isStrength == 1) %>%
        filter(resp1_diff== 1) %>%
        group_by(stimLev, sub) %>%
        summarise(diff_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(diff_high_conf = mean(diff_prop, na.rm = T), 
                  diff_high_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()

diff_average_low_confidence_ADT <- ADT_conf_ws %>%
        filter(isStrength == 0) %>%
        filter(resp1_diff== 1) %>%
        group_by(stimLev, sub) %>%
        summarise(diff_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(diff_low_conf = mean(diff_prop, na.rm = T), 
                  diff_low_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()


same_average_high_confidence_ADT <- ADT_conf_ws %>%
        filter(isStrength == 1) %>%
        filter(resp1_diff== 0) %>%
        group_by(stimLev, sub) %>%
        summarise(same_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(same_high_conf = mean(same_prop, na.rm = T), 
                  same_high_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()

same_average_low_confidence_ADT <- ADT_conf_ws %>%
        filter(isStrength == 0) %>%
        filter(resp1_diff== 0) %>%
        group_by(stimLev, sub) %>%
        summarise(same_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(same_low_conf = mean(same_prop, na.rm = T), 
                  same_low_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()


combined_conf_grip_ADT <- bind_rows(
        mutate(diff_average_high_confidence_ADT, condition = "Different - High Grip"),
        mutate(diff_average_low_confidence_ADT, condition = "Different - Low Grip" ),
        mutate(same_average_high_confidence_ADT, condition = "Same - High Grip"),
        mutate(same_average_low_confidence_ADT, condition = "Same - Low Grip")
)
  
    # Specify the desired order of levels for 'stimLev'
    desired_order <- c("0", "4", "8", "32", "128")
    
    # Convert 'stimLev' to factor with the desired order
    combined_conf_grip_ADT$stimLev <- factor(combined_conf_grip_ADT$stimLev, levels = desired_order)
    
    # Sort the combined dataset by stimLev within each condition
    combined_conf_grip_ADT <- combined_conf_grip_ADT %>%
        arrange(condition, stimLev)
    


ggplot(combined_conf_grip_ADT, aes(x = stimLev, color = condition)) +
  geom_point(aes(y = diff_high_conf), size = 3) +
  geom_line(aes(y = diff_high_conf, group = condition), size = 1.2) +
  geom_linerange(aes(ymin = diff_high_conf - diff_high_conf_se, 
                     ymax = diff_high_conf + diff_high_conf_se, group = condition), 
                 width = 0.2, linetype = "solid") +
  geom_point(aes(y = diff_low_conf), size = 3) +
  geom_line(aes(y = diff_low_conf, group = condition), size = 1.2, linetype = "solid") +
  geom_linerange(aes(ymin = diff_low_conf - diff_low_conf_se, 
                     ymax = diff_low_conf + diff_low_conf_se, group = condition), 
                 width = 0.2, linetype = "solid") +
  geom_point(aes(y = same_high_conf), size = 3) +
  geom_line(aes(y = same_high_conf, group = condition), size = 1.2) +
  geom_linerange(aes(ymin = same_high_conf - same_high_conf_se, 
                     ymax = same_high_conf + same_high_conf_se, group = condition), 
                  linetype = "solid") +
  geom_point(aes(y = same_low_conf), size = 3) +
  geom_line(aes(y = same_low_conf, group = condition), size = 1.2, linetype = "solid") +
  geom_linerange(aes(ymin = same_low_conf - same_low_conf_se, 
                     ymax = same_low_conf + same_low_conf_se, group = condition), 
                 width = 0.2, linetype = "solid") +
  ggtitle("Average of Confidence Response - ADT") +
  labs(caption = "Error bars represent within-subject SEM", x = "Oddball Level", y = "Confidence Rating") +
  scale_color_manual(values = c("#BC3C29FF", "#ea9999", "#0072B5FF", "#CCE6FF"),
                     name = "Response Type") +
  ylim(2,4)+
  scale_linetype_manual(values = c("solid", "solid"), name = "Condition",
                        labels = c("High Grip", "Low Grip")) 

3 VDT

3.1 Prportion of Responding ‘Different’

3.1.1 within-subject SEM

# calculate subject mean
VDT_acc_ws <- VDT %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1_diff, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
VDT_acc_ws <- VDT_acc_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

VDT_acc_ws <- VDT_acc_ws %>%
  mutate(acc_diff = sub_mean- grand_mean) 

# Calculate mean accuracy and standard error for 'Different'
Diff_prop_VDT <- VDT_acc_ws %>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(diff=mean(diff_sub), diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate the proportion of 'Same'
Same_prop_VDT <- VDT_acc_ws %>%
    group_by(sub, stimLev) %>%
    summarise(same_sub = 1-mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(not_diff=mean(same_sub), not_diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

# Combine both datasets for plotting
combined_VDT <- bind_rows(
  mutate(Diff_prop_VDT, condition = "Different", prop = diff),
  mutate(Same_prop_VDT, condition = "Same", prop = not_diff)
)

# Specify the desired order of levels for 'stimLev'
desired_order <- c("0", "0.04", "0.08", "0.16", "0.32")

# Convert 'stimLev' to factor with the desired order
combined_VDT$stimLev <- factor(combined_VDT$stimLev, levels = desired_order)

# Plotting with the modified 'stimLev'
ggplot(combined_VDT, aes(x = stimLev, y = prop, group = condition, color = condition)) +
  geom_point(size = 2) +
  geom_line(size = 1.2) +
  geom_linerange(mapping = aes(ymin = prop - ifelse(condition == "Different", diff_se, not_diff_se), 
                                ymax = prop + ifelse(condition == "Different", diff_se, not_diff_se))) +
  ylim(0, 1) +
  ggtitle("Proportion of Responding 'Different' and 'Same' - VDT'") +
  xlab("Contrast Level") +
  ylab("Proportion") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("#BC3C29FF", "#0072B5FF")) +
  guides(color = guide_legend(title = "condition"))

3.1.2 Handgrip manipulation

3.1.2.1 Binary

# calculate subject mean
VDT_acc_ws <- VDT %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1_diff, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
VDT_acc_ws <- VDT_acc_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

VDT_acc_ws <- VDT_acc_ws %>%
  mutate(acc_diff = sub_mean- grand_mean) 

# Calculate mean accuracy and standard error for 'Different'
Diff_prop_high_grip_VDT <- VDT_acc_ws %>%
    filter(isStrength==1)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(diff=mean(diff_sub), diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

Diff_prop_low_grip_VDT <- VDT_acc_ws %>%
    filter(isStrength==0)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(diff=mean(diff_sub), diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate the proportion of 'Same'
Same_prop_high_grip_VDT <- VDT_acc_ws %>%
    filter(isStrength==1) %>%
    group_by(sub, stimLev) %>%
    summarise(same_sub = 1-mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(not_diff=mean(same_sub), not_diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

Same_prop_low_grip_VDT <- VDT_acc_ws %>%
    filter(isStrength==0) %>%
    group_by(sub, stimLev) %>%
    summarise(same_sub = 1-mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(not_diff=mean(same_sub), not_diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

# Combine both datasets for plotting
combined_grip_VDT <- bind_rows(
  mutate(Diff_prop_high_grip_VDT, condition = "Different - High Grip", prop = diff),
  mutate(Diff_prop_low_grip_VDT, condition = "Different - Low Grip", prop = diff),
  mutate(Same_prop_high_grip_VDT, condition = "Same - High Grip", prop = not_diff),
  mutate(Same_prop_low_grip_VDT, condition = "Same - Low Grip", prop = not_diff)
)

# Specify the desired order of levels for 'stimLev'
desired_order <- c("0", "0.04", "0.08", "0.16", "0.32")

# Convert 'stimLev' to factor with the desired order
combined_grip_VDT$stimLev <- factor(combined_grip_VDT$stimLev, levels = desired_order)

# Plotting
ggplot(combined_grip_VDT, aes(x = stimLev, y = prop, group = condition, color = condition)) +
  geom_point( size = 2) +
  geom_line(size = 1.2) +
  geom_linerange(mapping = aes(ymin = prop - ifelse(condition == "Different - High Grip" | condition == "Different - Low Grip", diff_se, not_diff_se), 
                               ymax = prop + ifelse(condition == "Different - High Grip" | condition == "Different - Low Grip", diff_se, not_diff_se)), show.legend = FALSE) +
  ylim(0, 1) +
  ggtitle("Proportion of Responding 'Different' and 'Same' - VDT'") +
  xlab("Contrast Level") +
  ylab("Proportion") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("Different - High Grip" = "#BC3C29FF", "Different - Low Grip" = "#ea9999", "Same - High Grip" = "#0072B5FF", "Same - Low Grip" = "#CCE6FF")) +
  scale_linetype_manual(values = c("Different - High Grip" = "solid", "Different - Low Grip" = "solid", "Same - High Grip" = "solid", "Same - Low Grip" = "solid")) +
  guides(color = guide_legend(title = "Response Type", ncol = 1), linetype = "none")

3.1.2.2 Continuous

library(ggplot2)

lower_limit <- 0  # Lower limit
upper_limit <- 0.6  # Upper limit

# Filter the data based on the auc_rel_mvc limits
VDT_acc_ws <- VDT_acc_ws %>%
    filter(auc_rel_mvc <= upper_limit)
VDT_acc_ws <- VDT_acc_ws %>%
    filter(auc_rel_mvc <= upper_limit)

# Specify the desired order of levels for 'stimLev'
desired_order <- c("0", "0.04", "0.08", "0.16", "0.32")

# Convert 'stimLev' to factor with the desired order
VDT_acc_ws$stimLev <- factor(VDT_acc_ws$stimLev, levels = desired_order)


# Define the plot aesthetics
ggplot(VDT_acc_ws, aes(x = stimLev, y = auc_rel_mvc, color = factor(resp1_diff))) +
  
  # Add points for scatter plot
  #geom_point(alpha = 0.7) +
  
  # Set plot title and axis labels
  ggtitle("Relationship between Stimulus Level, Grip Force, and Same or Different - VDT") +
  xlab("Contrast Level") +
  ylab("Relative Grip") +
  geom_jitter(alpha = 0.4, width = 0.2, height = 0.05) +

 # Customize legend
  scale_color_manual(values = c("#0072B5FF", "#BC3C29FF"), labels = c("Same (0)", "Different (1)")) +
  scale_size_manual(values = c(3, 6), labels = c("Same (0)", "Different (1)")) +
  guides(color = guide_legend(title = "Response Type"),
         size = guide_legend(title = "Response Type"))

3.1.2.3 Low vs High handgrip for each subject across oddball levels

VDT$gf_trPer <- as.factor(VDT$gf_trPer)
VDT$gf_trPer <- recode_factor(VDT$gf_trPer, '0.05' = "Low", '0.4' = "High")
# Calculate individual subject responses
subject_responses_VDT_2 <- VDT %>%
  group_by(sub, stimLev, gf_trPer) %>%
  summarise(response = mean(resp1_diff, na.rm = TRUE)) %>%
  arrange(gf_trPer) %>%
  ungroup()

# Replace 'sub' with 'gf_trPer' for X-axis labels
subject_responses_VDT_2$gf_trPer <- factor(subject_responses_VDT_2$gf_trPer, levels = c("Low", "High"))

# Plotting
p10_VDT <- ggplot(subject_responses_VDT_2, aes(x = gf_trPer, y = response, color=gf_trPer, group = sub, text =   as.character(sub))) +
  geom_line(color = "gray") +
  geom_point(size = 2) +
  ggtitle("Proportion of Responding 'Different'") +
  scale_color_manual(values = c("#0072B5FF", "#BC3C29FF")) +
  xlab("Grip Level") +
  ylab("P('Different')") +
  facet_wrap(~stimLev, scales = "free_x", ncol = length(unique(subject_responses_VDT_2$stimLev)))+
  guides(color = guide_legend(title = "Grip Level"))

ggplotly(p10_VDT, tooltip = "text")

3.2 Reaction time

3.2.1 Using Median with within-subject SEM

# calculate subject mean
VDT_RT_ws <- VDT %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1RT, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
VDT_RT_ws <- VDT_RT_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

VDT_RT_ws <- VDT_RT_ws %>%
  mutate(RT_diff = sub_mean- grand_mean) 


# Calculate median RT for different responses
VDT_diff_RT_ws <- VDT_RT_ws %>%
  filter(resp1_diff == 1) %>%
  group_by(stimLev,sub) %>%
  summarise(diff_sub= median(resp1RT), RT_diff) %>%
  ungroup() %>%
  group_by(stimLev) %>%
  summarise(medRT_diff = median(diff_sub),
            medRT_diff_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
  distinct()

# Calculate median RT for same responses
VDT_same_RT_ws <- VDT_RT_ws %>%
  filter(resp1_diff == 0) %>%
  group_by(stimLev,sub) %>%
  summarise(diff_sub= median(resp1RT), RT_diff) %>%
  ungroup() %>%
  group_by(stimLev) %>%
  summarise(medRT_same = median(diff_sub),
            medRT_same_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
  distinct()

# Combine both datasets for plotting
combined_RT_VDT <- bind_rows(
  mutate(VDT_diff_RT_ws, condition = "Different"),
  mutate(VDT_same_RT_ws, condition = "Same")
)


# Specify the desired order of levels for 'stimLev'
    desired_order <- c("0", "0.04", "0.08", "0.16", "0.32")

# Convert 'stimLev' to factor with the desired order
combined_RT_VDT$stimLev <- factor(combined_RT_VDT$stimLev, levels = desired_order)

#Sort the combined dataset by stimLev within each condition
combined_RT_VDT <- combined_RT_VDT %>%
  arrange(condition, stimLev)

# Plotting
ggplot(combined_RT_VDT, aes(x = stimLev, group = condition, color = condition, linetype= condition)) +
  geom_point(aes(y = ifelse(condition == "Different", medRT_diff , medRT_same)), size = 2) +
  geom_line(aes(y = ifelse(condition == "Different", medRT_diff, medRT_same)), size = 1.2) +
  ylim(min(c(combined_RT_VDT$medRT_diff, combined_RT_VDT$medRT_same)), max(c(combined_RT_VDT$medRT_diff, combined_RT_VDT$medRT_same))) +
  ggtitle("Median of Reaction Time for Same and Different Responses - VDT") +
  geom_linerange(mapping = aes(ymin = medRT_diff - medRT_diff_se, ymax = medRT_diff + medRT_diff_se, linetype = condition), data = filter(combined_RT_VDT, condition == "Different")) +
  geom_linerange(mapping = aes(ymin = medRT_same - medRT_same_se, ymax = medRT_same + medRT_same_se, linetype = condition), data = filter(combined_RT_VDT, condition == "Same")) +
  labs(caption = "Error bars represent within-subject SEM") +
  xlab("Contrast Level") +
  ylab("Reaction Time (S)") +
  scale_color_manual(values = c( "#BC3C29FF", "#0072B5FF")) +
  scale_linetype_manual(values = c("Different" = "solid", "Same" = "solid")) 

3.2.2 Handgrip manipulation

3.2.2.1 Binary

# calculate subject mean
VDT_RT_ws <- VDT %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1RT, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calcualte the grand mean
VDT_RT_ws <- VDT_RT_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

VDT_RT_ws <- VDT_RT_ws %>%
  mutate(RT_diff = sub_mean- grand_mean) 

# Calculate mean RT and standard error for  High 'Different'
RT_diff_high_grip_VDT <- VDT_RT_ws %>%
    filter(resp1_diff == 1) %>%
    filter(isStrength==1)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_high_diff=median(diff_sub), medRT_high_diff_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate mean RT and standard error for  Low 'Different'
RT_diff_low_grip_VDT <- VDT_RT_ws %>%
    filter(resp1_diff == 1) %>%
    filter(isStrength==0)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_low_diff=median(diff_sub), medRT_low_diff_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()


# Calculate mean RT and standard error for  High 'Same'
RT_same_high_grip_VDT <- VDT_RT_ws %>%
    filter(resp1_diff == 0) %>%
    filter(isStrength==1)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_high_same=median(diff_sub), medRT_high_same_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate mean RT and standard error for  Low 'Same'
RT_same_low_grip_VDT <- VDT_RT_ws %>%
    filter(resp1_diff == 0) %>%
    filter(isStrength==0)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_low_same=median(diff_sub), medRT_low_same_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()


# Combine both datasets for plotting
combined_RT_grip_VDT <- bind_rows(
  mutate(RT_diff_high_grip_VDT, condition = "Different - High Grip"),
  mutate(RT_diff_low_grip_VDT, condition = "Different - Low Grip"),
  mutate(RT_same_high_grip_VDT, condition = "Same - High Grip"),
  mutate(RT_same_low_grip_VDT, condition = "Same - Low Grip")
)

# Specify the desired order of levels for 'stimLev'
    desired_order <- c("0", "0.04", "0.08", "0.16", "0.32")

# Convert 'stimLev' to factor with the desired order
combined_RT_grip_VDT$stimLev <- factor(combined_RT_grip_VDT$stimLev, levels = desired_order)


# Plotting without jitter
ggplot(data = combined_RT_grip_VDT) +
  geom_point(aes(x = stimLev, y = medRT_high_diff, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_high_diff, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_high_diff - medRT_high_diff_se, 
                     ymax = medRT_high_diff + medRT_high_diff_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_low_diff, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_low_diff, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_low_diff - medRT_low_diff_se, 
                     ymax = medRT_low_diff + medRT_low_diff_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_high_same, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_high_same, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_high_same - medRT_high_same_se, 
                     ymax = medRT_high_same + medRT_high_same_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_low_same, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_low_same, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_low_same - medRT_low_same_se, 
                     ymax = medRT_low_same + medRT_low_same_se, color = condition), 
                 linetype = "solid") +
  
  ggtitle("Median of Reaction Time for Same and Different Responses - VDT") +
  xlab("Contrast Level") +
  ylab("Reaction Time") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("#BC3C29FF", "#ea9999", "#0072B5FF", "#CCE6FF"),
                      name = "Condition") 

3.3 Confidence

3.3.1 Within-subject SEM

# calculate subject mean
VDT_conf_ws <- VDT %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp2, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
VDT_conf_ws <- VDT_conf_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

VDT_conf_ws <- VDT_conf_ws %>%
  mutate(conf_diff = sub_mean- grand_mean) 

diff_average_confidence_VDT <- VDT_conf_ws %>%
        filter(resp1_diff== 1) %>%
        group_by(stimLev, sub) %>%
        summarise(diff_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(diff_conf = mean(diff_prop, na.rm = T), 
                  diff_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()

same_average_confidence_VDT <- VDT_conf_ws %>%
        filter(resp1_diff== 0) %>%
        group_by(stimLev, sub) %>%
        summarise(same_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(same_conf = mean(same_prop), 
                  same_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()

  combined_prop_VDT <- bind_rows(
        mutate(diff_average_confidence_VDT, condition = "Different"),
        mutate(same_average_confidence_VDT, condition = "Same")
    )
  
    # Specify the desired order of levels for 'stimLev'
    desired_order <- c("0", "0.04", "0.08", "0.16", "0.32")
    
    # Convert 'stimLev' to factor with the desired order
    combined_prop_VDT$stimLev <- factor(combined_prop_VDT$stimLev, levels = desired_order)
    
    # Sort the combined dataset by stimLev within each condition
    combined_prop_VDT <- combined_prop_VDT %>%
        arrange(condition, stimLev)
    
    # Plotting
    ggplot(combined_prop_VDT, aes(x = stimLev, group = condition, color = condition)) +
        geom_point(aes(y = ifelse(condition == "Different", diff_conf, same_conf)), size = 2) +
    geom_line(aes(y = ifelse(condition == "Different", diff_conf, same_conf)), size = 1.2) +
        ylim(2, 4) +
        ggtitle("Average of Confidence Response - VDT") +
        geom_linerange(mapping = aes(ymin = diff_conf - diff_conf_se, ymax = diff_conf + diff_conf_se, linetype = condition), data = filter(combined_prop_VDT, condition == "Different")) +
    geom_linerange(mapping = aes(ymin = same_conf - same_conf_se, ymax = same_conf + same_conf_se, linetype = condition), data = filter(combined_prop_VDT, condition == "Same")) +
        labs(caption = "Error bars represent within-subject SEM") +
        xlab("Contrast Level") +
        ylab("Confidence Rating") +
        scale_color_manual(values = c("Different" = "#BC3C29FF",
                                      "Same" = "#0072B5FF"
                                      ),
                           labels = c("Different",
                                      "Same"
                                      )) +
        scale_linetype_manual(values = c("Different" = "solid", "Same" = "solid"))

3.3.2 Handgrip manipulation

3.3.2.1 Binary

# calculate subject mean
VDT_conf_ws <- VDT %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp2, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
VDT_conf_ws <- VDT_conf_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

VDT_conf_ws <- VDT_conf_ws %>%
  mutate(conf_diff = sub_mean- grand_mean) 

diff_average_high_confidence_VDT <- VDT_conf_ws %>%
        filter(isStrength == 1) %>%
        filter(resp1_diff== 1) %>%
        group_by(stimLev, sub) %>%
        summarise(diff_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(diff_high_conf = mean(diff_prop, na.rm = T), 
                  diff_high_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()

diff_average_low_confidence_VDT <- VDT_conf_ws %>%
        filter(isStrength == 0) %>%
        filter(resp1_diff== 1) %>%
        group_by(stimLev, sub) %>%
        summarise(diff_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(diff_low_conf = mean(diff_prop, na.rm = T), 
                  diff_low_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()


same_average_high_confidence_VDT <- VDT_conf_ws %>%
        filter(isStrength == 1) %>%
        filter(resp1_diff== 0) %>%
        group_by(stimLev, sub) %>%
        summarise(same_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(same_high_conf = mean(same_prop, na.rm = T), 
                  same_high_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()

same_average_low_confidence_VDT <- VDT_conf_ws %>%
        filter(isStrength == 0) %>%
        filter(resp1_diff== 0) %>%
        group_by(stimLev, sub) %>%
        summarise(same_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(same_low_conf = mean(same_prop, na.rm = T), 
                  same_low_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()


combined_conf_grip_VDT <- bind_rows(
        mutate(diff_average_high_confidence_VDT, condition = "Different - High Grip"),
        mutate(diff_average_low_confidence_VDT, condition = "Different - Low Grip" ),
        mutate(same_average_high_confidence_VDT, condition = "Same - High Grip"),
        mutate(same_average_low_confidence_VDT, condition = "Same - Low Grip")
)
  
    # Specify the desired order of levels for 'stimLev'
    desired_order <- c("0", "0.04", "0.08", "0.16", "0.32")
    
    # Convert 'stimLev' to factor with the desired order
    combined_conf_grip_VDT$stimLev <- factor(combined_conf_grip_VDT$stimLev, levels = desired_order)
    
    # Sort the combined dataset by stimLev within each condition
    combined_conf_grip_VDT <- combined_conf_grip_VDT %>%
        arrange(condition, stimLev)
    


ggplot(combined_conf_grip_VDT, aes(x = stimLev, color = condition)) +
  geom_point(aes(y = diff_high_conf), size = 3) +
  geom_line(aes(y = diff_high_conf, group = condition), size = 1.2) +
  geom_linerange(aes(ymin = diff_high_conf - diff_high_conf_se, 
                     ymax = diff_high_conf + diff_high_conf_se, group = condition), 
                 width = 0.2, linetype = "solid") +
  geom_point(aes(y = diff_low_conf), size = 3) +
  geom_line(aes(y = diff_low_conf, group = condition), size = 1.2, linetype = "solid") +
  geom_linerange(aes(ymin = diff_low_conf - diff_low_conf_se, 
                     ymax = diff_low_conf + diff_low_conf_se, group = condition), 
                 width = 0.2, linetype = "solid") +
  geom_point(aes(y = same_high_conf), size = 3) +
  geom_line(aes(y = same_high_conf, group = condition), size = 1.2) +
  geom_linerange(aes(ymin = same_high_conf - same_high_conf_se, 
                     ymax = same_high_conf + same_high_conf_se, group = condition), 
                  linetype = "solid") +
  geom_point(aes(y = same_low_conf), size = 3) +
  geom_line(aes(y = same_low_conf, group = condition), size = 1.2, linetype = "solid") +
  geom_linerange(aes(ymin = same_low_conf - same_low_conf_se, 
                     ymax = same_low_conf + same_low_conf_se, group = condition), 
                 width = 0.2, linetype = "solid") +
  ggtitle("Average of Confidence Response - VDT") +
  ylim(2,4)+
  labs(caption = "Error bars represent within-subject SEM", x = "Oddball Level", y = "Confidence Rating") +
  scale_color_manual(values = c("#BC3C29FF", "#ea9999", "#0072B5FF", "#CCE6FF"),
                     name = "Response Type") +
  scale_linetype_manual(values = c("solid", "solid"), name = "Condition",
                        labels = c("High Grip", "Low Grip")) 

4 CDT

4.1 Prportion of Responding ‘Different’

4.1.1 within-subject SEM

# calculate subject mean
CDT_acc_ws <- CDT %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1_diff, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
CDT_acc_ws <- CDT_acc_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

CDT_acc_ws <- CDT_acc_ws %>%
  mutate(acc_diff = sub_mean- grand_mean) 

# Calculate mean accuracy and standard error for 'Different'
Diff_prop_CDT <- CDT_acc_ws %>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(diff=mean(diff_sub), diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate the proportion of 'Same'
Same_prop_CDT <- CDT_acc_ws %>%
    group_by(sub, stimLev) %>%
    summarise(same_sub = 1-mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(not_diff=mean(same_sub), not_diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

# Combine both datasets for plotting
combined_CDT <- bind_rows(
  mutate(Diff_prop_CDT, condition = "Different", prop = diff),
  mutate(Same_prop_CDT, condition = "Same", prop = not_diff)
)

# Specify the desired order of levels for 'stimLev'
desired_order <- c("0", "5", "20", "45", "90")

# Convert 'stimLev' to factor with the desired order
combined_CDT$stimLev <- factor(combined_CDT$stimLev, levels = desired_order)

# Plotting with the modified 'stimLev'
ggplot(combined_CDT, aes(x = stimLev, y = prop, group = condition, color = condition)) +
  geom_point(size = 2) +
  geom_line(size = 1.2) +
  geom_linerange(mapping = aes(ymin = prop - ifelse(condition == "Different", diff_se, not_diff_se), 
                                ymax = prop + ifelse(condition == "Different", diff_se, not_diff_se))) +
  ylim(0, 1) +
  ggtitle("Proportion of Responding 'Different' and 'Same' - CDT'") +
  xlab("Degree") +
  ylab("Proportion") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("#BC3C29FF", "#0072B5FF")) +
  guides(color = guide_legend(title = "condition"))

4.1.2 Handgrip manipulation

4.1.2.1 Binary

# calculate subject mean
CDT_acc_ws <- CDT %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1_diff, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
CDT_acc_ws <- CDT_acc_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

CDT_acc_ws <- CDT_acc_ws %>%
  mutate(acc_diff = sub_mean- grand_mean) 

# Calculate mean accuracy and standard error for 'Different'
Diff_prop_high_grip_CDT <- CDT_acc_ws %>%
    filter(isStrength==1)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(diff=mean(diff_sub), diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

Diff_prop_low_grip_CDT <- CDT_acc_ws %>%
    filter(isStrength==0)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(diff=mean(diff_sub), diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate the proportion of 'Same'
Same_prop_high_grip_CDT <- CDT_acc_ws %>%
    filter(isStrength==1) %>%
    group_by(sub, stimLev) %>%
    summarise(same_sub = 1-mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(not_diff=mean(same_sub), not_diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

Same_prop_low_grip_CDT <- CDT_acc_ws %>%
    filter(isStrength==0) %>%
    group_by(sub, stimLev) %>%
    summarise(same_sub = 1-mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(not_diff=mean(same_sub), not_diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

# Combine both datasets for plotting
combined_grip_CDT <- bind_rows(
  mutate(Diff_prop_high_grip_CDT, condition = "Different - High Grip", prop = diff),
  mutate(Diff_prop_low_grip_CDT, condition = "Different - Low Grip", prop = diff),
  mutate(Same_prop_high_grip_CDT, condition = "Same - High Grip", prop = not_diff),
  mutate(Same_prop_low_grip_CDT, condition = "Same - Low Grip", prop = not_diff)
)

# Specify the desired order of levels for 'stimLev'
desired_order <- c("0", "5", "20", "45", "90")

# Convert 'stimLev' to factor with the desired order
combined_grip_CDT$stimLev <- factor(combined_grip_CDT$stimLev, levels = desired_order)

# Plotting
ggplot(combined_grip_CDT, aes(x = stimLev, y = prop, group = condition, color = condition)) +
  geom_point( size = 2) +
  geom_line(size = 1.2) +
  geom_linerange(mapping = aes(ymin = prop - ifelse(condition == "Different - High Grip" | condition == "Different - Low Grip", diff_se, not_diff_se), 
                               ymax = prop + ifelse(condition == "Different - High Grip" | condition == "Different - Low Grip", diff_se, not_diff_se)), show.legend = FALSE) +
  ylim(0, 1) +
  ggtitle("Proportion of Responding 'Different' and 'Same' - CDT'") +
  xlab("Degree") +
  ylab("Proportion") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("Different - High Grip" = "#BC3C29FF", "Different - Low Grip" = "#ea9999", "Same - High Grip" = "#0072B5FF", "Same - Low Grip" = "#CCE6FF")) +
  scale_linetype_manual(values = c("Different - High Grip" = "solid", "Different - Low Grip" = "solid", "Same - High Grip" = "solid", "Same - Low Grip" = "solid")) +
  guides(color = guide_legend(title = "Response Type", ncol = 1), linetype = "none")

4.1.2.2 Continuous

lower_limit <- 0  # Lower limit
upper_limit <- 0.6  # Upper limit

# Filter the data based on the auc_rel_mvc limits
CDT_acc_ws <- CDT_acc_ws %>%
    filter(auc_rel_mvc <= upper_limit)
CDT_acc_ws <- CDT_acc_ws %>%
    filter(auc_rel_mvc <= upper_limit)

# Specify the desired order of levels for 'stimLev'
desired_order <- c("0", "5", "20", "45", "90")

# Convert 'stimLev' to factor with the desired order
CDT_acc_ws$stimLev <- factor(CDT_acc_ws$stimLev, levels = desired_order)


# Define the plot aesthetics
ggplot(CDT_acc_ws, aes(x = stimLev, y = auc_rel_mvc, color = factor(resp1_diff))) +
  
  # Add points for scatter plot
  #geom_point(alpha = 0.7) +
  
  # Set plot title and axis labels
  ggtitle("Relationship between Stimulus Level, Grip Force, and Same or Different - CDT") +
  xlab("Degree") +
  ylab("Relative Grip") +
  geom_jitter(alpha = 0.4, width = 0.2, height = 0.05) +

 # Customize legend
  scale_color_manual(values = c("#0072B5FF", "#BC3C29FF"), labels = c("Same (0)", "Different (1)")) +
  scale_size_manual(values = c(3, 6), labels = c("Same (0)", "Different (1)")) +
  guides(color = guide_legend(title = "Response Type"),
         size = guide_legend(title = "Response Type"))

4.1.2.3 Low vs High handgrip for each subject across oddball levels

CDT$gf_trPer <- as.factor(CDT$gf_trPer)
CDT$gf_trPer <- recode_factor(CDT$gf_trPer, '0.05' = "Low", '0.4' = "High")
# Calculate individual subject responses
subject_responses_CDT_2 <- CDT %>%
  group_by(sub, stimLev, gf_trPer) %>%
  summarise(response = mean(resp1_diff, na.rm = TRUE)) %>%
  arrange(gf_trPer) %>%
  ungroup()

# Replace 'sub' with 'gf_trPer' for X-axis labels
subject_responses_CDT_2$gf_trPer <- factor(subject_responses_CDT_2$gf_trPer, levels = c("Low", "High"))

# Plotting
p10_CDT <- ggplot(subject_responses_CDT_2, aes(x = gf_trPer, y = response, color=gf_trPer, group = sub, text =   as.character(sub))) +
  geom_line(color = "gray") +
  geom_point(size = 2) +
  ggtitle("Proportion of Responding 'Different'") +
  scale_color_manual(values = c("#0072B5FF", "#BC3C29FF")) +
  xlab("Grip Level") +
  ylab("P('Different')") +
  facet_wrap(~stimLev, scales = "free_x", ncol = length(unique(subject_responses_CDT_2$stimLev)))+
  guides(color = guide_legend(title = "Grip Level"))

ggplotly(p10_CDT, tooltip = "text")

4.2 Reaction time

4.2.1 Using Median with within-subject SEM

# calculate subject mean
CDT_RT_ws <- CDT %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1RT, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
CDT_RT_ws <- CDT_RT_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

CDT_RT_ws <- CDT_RT_ws %>%
  mutate(RT_diff = sub_mean- grand_mean) 


# Calculate median RT for different responses
CDT_diff_RT_ws <- CDT_RT_ws %>%
  filter(resp1_diff == 1) %>%
  group_by(stimLev,sub) %>%
  summarise(diff_sub= median(resp1RT), RT_diff) %>%
  ungroup() %>%
  group_by(stimLev) %>%
  summarise(medRT_diff = median(diff_sub),
            medRT_diff_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
  distinct()

# Calculate median RT for same responses
CDT_same_RT_ws <- CDT_RT_ws %>%
  filter(resp1_diff == 0) %>%
  group_by(stimLev,sub) %>%
  summarise(diff_sub= median(resp1RT), RT_diff) %>%
  ungroup() %>%
  group_by(stimLev) %>%
  summarise(medRT_same = median(diff_sub),
            medRT_same_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
  distinct()

# Combine both datasets for plotting
combined_RT_CDT <- bind_rows(
  mutate(CDT_diff_RT_ws, condition = "Different"),
  mutate(CDT_same_RT_ws, condition = "Same")
)


# Specify the desired order of levels for 'stimLev'
    desired_order <- c("0", "5", "20", "45", "90")

# Convert 'stimLev' to factor with the desired order
combined_RT_CDT$stimLev <- factor(combined_RT_CDT$stimLev, levels = desired_order)

#Sort the combined dataset by stimLev within each condition
combined_RT_CDT <- combined_RT_CDT %>%
  arrange(condition, stimLev)

# Plotting
ggplot(combined_RT_CDT, aes(x = stimLev, group = condition, color = condition, linetype= condition)) +
  geom_point(aes(y = ifelse(condition == "Different", medRT_diff , medRT_same)), size = 2) +
  geom_line(aes(y = ifelse(condition == "Different", medRT_diff, medRT_same)), size = 1.2) +
  ylim(min(c(combined_RT_CDT$medRT_diff, combined_RT_CDT$medRT_same)), max(c(combined_RT_CDT$medRT_diff, combined_RT_CDT$medRT_same))) +
  ggtitle("Median of Reaction Time for Same and Different Responses - CDT") +
  geom_linerange(mapping = aes(ymin = medRT_diff - medRT_diff_se, ymax = medRT_diff + medRT_diff_se, linetype = condition), data = filter(combined_RT_CDT, condition == "Different")) +
  geom_linerange(mapping = aes(ymin = medRT_same - medRT_same_se, ymax = medRT_same + medRT_same_se, linetype = condition), data = filter(combined_RT_CDT, condition == "Same")) +
  labs(caption = "Error bars represent within-subject SEM") +
  xlab("Degree") +
  ylab("Reaction Time (S)") +
  scale_color_manual(values = c( "#BC3C29FF", "#0072B5FF")) +
  scale_linetype_manual(values = c("Different" = "solid", "Same" = "solid")) 

4.2.2 Handgrip manipulation

4.2.2.1 Binary

# calculate subject mean
CDT_RT_ws <- CDT %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1RT, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calcualte the grand mean
CDT_RT_ws <- CDT_RT_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

CDT_RT_ws <- CDT_RT_ws %>%
  mutate(RT_diff = sub_mean- grand_mean) 

# Calculate mean RT and standard error for  High 'Different'
RT_diff_high_grip_CDT <- CDT_RT_ws %>%
    filter(resp1_diff == 1) %>%
    filter(isStrength==1)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_high_diff=median(diff_sub), medRT_high_diff_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate mean RT and standard error for  Low 'Different'
RT_diff_low_grip_CDT <- CDT_RT_ws %>%
    filter(resp1_diff == 1) %>%
    filter(isStrength==0)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_low_diff=median(diff_sub), medRT_low_diff_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()


# Calculate mean RT and standard error for  High 'Same'
RT_same_high_grip_CDT <- CDT_RT_ws %>%
    filter(resp1_diff == 0) %>%
    filter(isStrength==1)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_high_same=median(diff_sub), medRT_high_same_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate mean RT and standard error for  Low 'Same'
RT_same_low_grip_CDT <- CDT_RT_ws %>%
    filter(resp1_diff == 0) %>%
    filter(isStrength==0)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_low_same=median(diff_sub), medRT_low_same_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()


# Combine both datasets for plotting
combined_RT_grip_CDT <- bind_rows(
  mutate(RT_diff_high_grip_CDT, condition = "Different - High Grip"),
  mutate(RT_diff_low_grip_CDT, condition = "Different - Low Grip"),
  mutate(RT_same_high_grip_CDT, condition = "Same - High Grip"),
  mutate(RT_same_low_grip_CDT, condition = "Same - Low Grip")
)

# Specify the desired order of levels for 'stimLev'
    desired_order <- c("0", "5", "20", "45", "90")

# Convert 'stimLev' to factor with the desired order
combined_RT_grip_CDT$stimLev <- factor(combined_RT_grip_CDT$stimLev, levels = desired_order)


# Plotting without jitter
ggplot(data = combined_RT_grip_CDT) +
  geom_point(aes(x = stimLev, y = medRT_high_diff, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_high_diff, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_high_diff - medRT_high_diff_se, 
                     ymax = medRT_high_diff + medRT_high_diff_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_low_diff, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_low_diff, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_low_diff - medRT_low_diff_se, 
                     ymax = medRT_low_diff + medRT_low_diff_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_high_same, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_high_same, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_high_same - medRT_high_same_se, 
                     ymax = medRT_high_same + medRT_high_same_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_low_same, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_low_same, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_low_same - medRT_low_same_se, 
                     ymax = medRT_low_same + medRT_low_same_se, color = condition), 
                 linetype = "solid") +
  
  ggtitle("Median of Reaction Time for Same and Different Responses - CDT") +
  xlab("Degree") +
  ylab("Median Reaction Time") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("#BC3C29FF", "#ea9999", "#0072B5FF", "#CCE6FF"),
                      name = "Condition") 

4.3 Confidence

4.3.1 Within-subject SEM

# calculate subject mean
CDT_conf_ws <- CDT %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp2, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
CDT_conf_ws <- CDT_conf_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

CDT_conf_ws <- CDT_conf_ws %>%
  mutate(conf_diff = sub_mean- grand_mean) 

diff_average_confidence_CDT <- CDT_conf_ws %>%
        filter(resp1_diff== 1) %>%
        group_by(stimLev, sub) %>%
        summarise(diff_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(diff_conf = mean(diff_prop, na.rm = T), 
                  diff_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()

same_average_confidence_CDT <- CDT_conf_ws %>%
        filter(resp1_diff== 0) %>%
        group_by(stimLev, sub) %>%
        summarise(same_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(same_conf = mean(same_prop), 
                  same_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()

  combined_prop_CDT <- bind_rows(
        mutate(diff_average_confidence_CDT, condition = "Different"),
        mutate(same_average_confidence_CDT, condition = "Same")
    )
  
    # Specify the desired order of levels for 'stimLev'
    desired_order <- c("0", "5", "20", "45", "90")
    
    # Convert 'stimLev' to factor with the desired order
    combined_prop_CDT$stimLev <- factor(combined_prop_CDT$stimLev, levels = desired_order)
    
    # Sort the combined dataset by stimLev within each condition
    combined_prop_CDT <- combined_prop_CDT %>%
        arrange(condition, stimLev)
    
    # Plotting
    ggplot(combined_prop_CDT, aes(x = stimLev, group = condition, color = condition)) +
        geom_point(aes(y = ifelse(condition == "Different", diff_conf, same_conf)), size = 2) +
    geom_line(aes(y = ifelse(condition == "Different", diff_conf, same_conf)), size = 1.2) +
        ylim(2, 4) +
        ggtitle("Average of Confidence Response - CDT") +
        geom_linerange(mapping = aes(ymin = diff_conf - diff_conf_se, ymax = diff_conf + diff_conf_se, linetype = condition), data = filter(combined_prop_CDT, condition == "Different")) +
    geom_linerange(mapping = aes(ymin = same_conf - same_conf_se, ymax = same_conf + same_conf_se, linetype = condition), data = filter(combined_prop_CDT, condition == "Same")) +
        labs(caption = "Error bars represent within-subject SEM") +
        xlab("Degree") +
        ylab("Confidence Rating") +
        scale_color_manual(values = c("Different" = "#BC3C29FF",
                                      "Same" = "#0072B5FF"
                                      ),
                           labels = c("Different",
                                      "Same"
                                      )) +
        scale_linetype_manual(values = c("Different" = "solid", "Same" = "solid"))

4.3.2 Handgrip manipulation

4.3.2.1 Binary

# calculate subject mean
CDT_conf_ws <- CDT %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp2, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
CDT_conf_ws <- CDT_conf_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

CDT_conf_ws <- CDT_conf_ws %>%
  mutate(conf_diff = sub_mean- grand_mean) 

diff_average_high_confidence_CDT <- CDT_conf_ws %>%
        filter(isStrength == 1) %>%
        filter(resp1_diff== 1) %>%
        group_by(stimLev, sub) %>%
        summarise(diff_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(diff_high_conf = mean(diff_prop, na.rm = T), 
                  diff_high_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()

diff_average_low_confidence_CDT <- CDT_conf_ws %>%
        filter(isStrength == 0) %>%
        filter(resp1_diff== 1) %>%
        group_by(stimLev, sub) %>%
        summarise(diff_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(diff_low_conf = mean(diff_prop, na.rm = T), 
                  diff_low_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()


same_average_high_confidence_CDT <- CDT_conf_ws %>%
        filter(isStrength == 1) %>%
        filter(resp1_diff== 0) %>%
        group_by(stimLev, sub) %>%
        summarise(same_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(same_high_conf = mean(same_prop, na.rm = T), 
                  same_high_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()

same_average_low_confidence_CDT <- CDT_conf_ws %>%
        filter(isStrength == 0) %>%
        filter(resp1_diff== 0) %>%
        group_by(stimLev, sub) %>%
        summarise(same_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(same_low_conf = mean(same_prop, na.rm = T), 
                  same_low_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()


combined_conf_grip_CDT <- bind_rows(
        mutate(diff_average_high_confidence_CDT, condition = "Different - High Grip"),
        mutate(diff_average_low_confidence_CDT, condition = "Different - Low Grip" ),
        mutate(same_average_high_confidence_CDT, condition = "Same - High Grip"),
        mutate(same_average_low_confidence_CDT, condition = "Same - Low Grip")
)
  
    # Specify the desired order of levels for 'stimLev'
    desired_order <- c("0", "5", "20", "45", "90")
    
    # Convert 'stimLev' to factor with the desired order
    combined_conf_grip_CDT$stimLev <- factor(combined_conf_grip_CDT$stimLev, levels = desired_order)
    
    # Sort the combined dataset by stimLev within each condition
    combined_conf_grip_CDT <- combined_conf_grip_CDT %>%
        arrange(condition, stimLev)
    


ggplot(combined_conf_grip_CDT, aes(x = stimLev, color = condition)) +
  geom_point(aes(y = diff_high_conf), size = 3) +
  geom_line(aes(y = diff_high_conf, group = condition), size = 1.2) +
  geom_linerange(aes(ymin = diff_high_conf - diff_high_conf_se, 
                     ymax = diff_high_conf + diff_high_conf_se, group = condition), 
                 width = 0.2, linetype = "solid") +
  geom_point(aes(y = diff_low_conf), size = 3) +
  geom_line(aes(y = diff_low_conf, group = condition), size = 1.2, linetype = "solid") +
  geom_linerange(aes(ymin = diff_low_conf - diff_low_conf_se, 
                     ymax = diff_low_conf + diff_low_conf_se, group = condition), 
                 width = 0.2, linetype = "solid") +
  geom_point(aes(y = same_high_conf), size = 3) +
  geom_line(aes(y = same_high_conf, group = condition), size = 1.2) +
  geom_linerange(aes(ymin = same_high_conf - same_high_conf_se, 
                     ymax = same_high_conf + same_high_conf_se, group = condition), 
                  linetype = "solid") +
  geom_point(aes(y = same_low_conf), size = 3) +
  geom_line(aes(y = same_low_conf, group = condition), size = 1.2, linetype = "solid") +
  geom_linerange(aes(ymin = same_low_conf - same_low_conf_se, 
                     ymax = same_low_conf + same_low_conf_se, group = condition), 
                 width = 0.2, linetype = "solid") +
  ggtitle("Average of Confidence Response - CDT") +
  labs(caption = "Error bars represent within-subject SEM", x = "Degree", y = "Confidence Rating") +
  scale_color_manual(values = c("#BC3C29FF", "#ea9999", "#0072B5FF", "#CCE6FF"),
                     name = "Response Type") +
  ylim(2,4)+
  scale_linetype_manual(values = c("solid", "solid"), name = "Condition",
                        labels = c("High Grip", "Low Grip")) 

5 MST

5.1 Prportion of Responding ‘Old’, ‘Similar’, and ‘New’

5.1.1 Within-subject SEM

# calculate subject mean
    MST_acc_ws <- MST %>%
      group_by(sub, stimlev_mst, resp1_diff) %>%
      mutate(sub_mean = mean(resp1_diff, na.rm=T)) %>%
      distinct() %>%
      ungroup()

# calculate the grand mean
    MST_acc_ws <- MST_acc_ws %>%
      group_by(sub) %>%
      mutate(grand_mean = mean(sub_mean)) %>%
      ungroup()

# subtract the subject mean and add grand mean for each cell

    MST_acc_ws <- MST_acc_ws %>%
      mutate(acc_diff = sub_mean- grand_mean) 

# Calculate mean proportion for old responses
    Old_prop_MST <- MST_acc_ws %>%
      group_by(stimlev_mst, sub) %>%
      mutate(resp1_old = case_when(resp1 == 'old' ~ 1, TRUE ~ 0)) %>%  # Corrected the syntax for case_when
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(prop_old = mean(resp1_old, na.rm = T),
                prop_old_se = sd(acc_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
        
# Calculate mean proportion for sim responses
    Sim_prop_MST <- MST_acc_ws %>%
      group_by(stimlev_mst, sub) %>%
      mutate(resp1_sim = case_when(resp1 == 'sim' ~ 1, TRUE ~ 0)) %>%  # Corrected the syntax for case_when
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(prop_sim = mean(resp1_sim, na.rm = T),
                prop_sim_se = sd(acc_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
    
# Calculate mean proportion for new responses
    New_prop_MST <- MST_acc_ws %>%
      group_by(stimlev_mst, sub) %>%
      mutate(resp1_new = case_when(resp1 == 'new' ~ 1, TRUE ~ 0)) %>%  # Corrected the syntax for case_when
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(prop_new = mean(resp1_new, na.rm = T),
                prop_new_se = sd(acc_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
    
# Combine both datasets for plotting
     combined_prop_MST <- bind_rows(
        mutate(Old_prop_MST, condition = "Old"),
        mutate(Sim_prop_MST, condition = "Similar"),
        mutate(New_prop_MST, condition = "New")
    )
    combined_prop_MST$stimlev_mst <- factor(combined_prop_MST$stimlev_mst, levels = desired_order)

# Specify the desired order of levels for 'stimlev_mst'
    desired_order <- c("0targ", "1lure", "2lure", "3lure", "4lure", "5foil")

# Convert 'stimlev_mst' to factor with the desired order
  combined_prop_MST$stimlev_mst <- factor(combined_prop_MST$stimlev_mst, levels = desired_order)



# Plotting
ggplot(combined_prop_MST, aes(x = stimlev_mst)) +
  geom_line(aes(y = prop_old, group = condition, color = "Old"), size = 1.2) +
  geom_line(aes(y = prop_sim, group = condition, color = "Similar"), size = 1.2) +
  geom_line(aes(y = prop_new, group = condition, color = "New"), size = 1.2) +
  geom_point(aes(y = prop_old, color = "Old"), size = 2) +
  geom_point(aes(y = prop_sim, color = "Similar"), size = 2) +
  geom_point(aes(y = prop_new, color = "New"), size = 2) +
  geom_linerange(aes(ymin = prop_old - prop_old_se, ymax = prop_old + prop_old_se), color = "#0072B5FF", size = 0.5) +
  geom_linerange(aes(ymin = prop_sim - prop_sim_se, ymax = prop_sim + prop_sim_se), color = "#20854EFF", size = 0.5) +
  geom_linerange(aes(ymin = prop_new - prop_new_se, ymax = prop_new + prop_new_se), color = "#BC3C29FF", size = 0.5) +
  ggtitle("Proportions of Old, Similar, and New Responses - MST") +
  xlab("Stimulus Level") +
  ylab("Proportion") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values=c("Old"="#0072B5FF", "New"="#BC3C29FF", "Similar"="#20854EFF")) +
  guides(color = guide_legend(title = "Response Type"))

5.1.2 Handgrip manipulation

5.1.2.1 Binary

# calculate subject mean
    MST_acc_ws <- MST %>%
      group_by(sub, stimlev_mst, resp1_diff) %>%
      mutate(sub_mean = mean(resp1_diff, na.rm=T)) %>%
      distinct() %>%
      ungroup()

# calculate the grand mean
    MST_acc_ws <- MST_acc_ws %>%
      group_by(sub) %>%
      mutate(grand_mean = mean(sub_mean)) %>%
      ungroup()

# subtract the subject mean and add grand mean for each cell

    MST_acc_ws <- MST_acc_ws %>%
      mutate(acc_diff = sub_mean- grand_mean) 

# Calculate mean proportion for high - old responses
    Old_prop_high_grip_MST <- MST_acc_ws %>%
      filter(isStrength==1) %>%
      group_by(stimlev_mst, sub) %>%
      mutate(resp1_old = case_when(resp1 == 'old' ~ 1, TRUE ~ 0)) %>% 
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(prop_old_high = mean(resp1_old, na.rm = T),
                prop_old_high_se = sd(acc_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()

# Calculate mean proportion for low - old responses
    Old_prop_low_grip_MST <- MST_acc_ws %>%
      filter(isStrength==0) %>%
      group_by(stimlev_mst, sub) %>%
      mutate(resp1_old = case_when(resp1 == 'old' ~ 1, TRUE ~ 0)) %>% 
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(prop_old_low = mean(resp1_old, na.rm = T),
                prop_old_low_se = sd(acc_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()    
    
# Calculate mean proportion for high-sim responses
    Sim_prop_high_grip_MST <- MST_acc_ws %>%
      filter(isStrength==1) %>%
      group_by(stimlev_mst, sub) %>%
      mutate(resp1_sim = case_when(resp1 == 'sim' ~ 1, TRUE ~ 0)) %>%  
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(prop_sim_high = mean(resp1_sim, na.rm = T),
                prop_sim_high_se = sd(acc_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
    
# Calculate mean proportion for low-sim responses
    Sim_prop_low_grip_MST <- MST_acc_ws %>%
      filter(isStrength==0) %>%
      group_by(stimlev_mst, sub) %>%
      mutate(resp1_sim = case_when(resp1 == 'sim' ~ 1, TRUE ~ 0)) %>%  
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(prop_sim_low = mean(resp1_sim, na.rm = T),
                prop_sim_low_se = sd(acc_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
        
# Calculate mean proportion for high-new responses
    New_prop_high_grip_MST <- MST_acc_ws %>%
      filter(isStrength==1) %>%
      group_by(stimlev_mst, sub) %>%
      mutate(resp1_new = case_when(resp1 == 'new' ~ 1, TRUE ~ 0)) %>%  
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(prop_new_high = mean(resp1_new, na.rm = T),
                prop_new_high_se = sd(acc_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
   
# Calculate mean proportion for low-new responses
    New_prop_low_grip_MST <- MST_acc_ws %>%
      filter(isStrength==0) %>%
      group_by(stimlev_mst, sub) %>%
      mutate(resp1_new = case_when(resp1 == 'new' ~ 1, TRUE ~ 0)) %>%  
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(prop_new_low = mean(resp1_new, na.rm = T),
                prop_new_low_se = sd(acc_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
    
    
# Combine both datasets for plotting
combined_grip_MST <- bind_rows(
  mutate(Old_prop_high_grip_MST, condition = "Old - High Grip", prop = prop_old_high),
  mutate(Old_prop_low_grip_MST, condition = "Old - Low Grip", prop = prop_old_low),
  mutate(Sim_prop_high_grip_MST, condition = "Similar - High Grip", prop = prop_sim_high),
  mutate(Sim_prop_low_grip_MST, condition = "Similar - Low Grip", prop = prop_sim_low),
  mutate(New_prop_high_grip_MST, condition = "New - High Grip", prop = prop_new_high),
  mutate(New_prop_low_grip_MST, condition = "New - Low Grip", prop = prop_new_low)
)

  combined_prop_MST$stimlev_mst <- factor(combined_prop_MST$stimlev_mst, levels = desired_order)

# Specify the desired order of levels for 'stimlev_mst'
    desired_order <- c("0targ", "1lure", "2lure", "3lure", "4lure", "5foil")

# Convert 'stimlev_mst' to factor with the desired order
  combined_prop_MST$stimlev_mst <- factor(combined_prop_MST$stimlev_mst, levels = desired_order)



# Plotting
ggplot(combined_grip_MST, aes(x = stimlev_mst, y = prop, group = condition, color = condition)) +
  geom_point(size = 2) +
  geom_line(size = 1.2) +
  geom_linerange(mapping = aes(ymin = prop - ifelse(grepl("Old", condition), prop_old_high_se, ifelse(grepl("Similar", condition), prop_sim_high_se, prop_new_high_se)),
                                  ymax = prop + ifelse(grepl("Old", condition), prop_old_high_se, ifelse(grepl("Similar", condition), prop_sim_high_se, prop_new_high_se)),
                                  color = condition),
                    show.legend = FALSE) +
  geom_linerange(mapping = aes(ymin = prop - ifelse(grepl("Old", condition), prop_old_low_se, ifelse(grepl("Similar", condition), prop_sim_low_se, prop_new_low_se)),
                                  ymax = prop + ifelse(grepl("Old", condition), prop_old_low_se, ifelse(grepl("Similar", condition), prop_sim_low_se, prop_new_low_se)),
                                  color = condition),
                    show.legend = FALSE) +
  ylim(0, 1) +
  ggtitle("Proportions of Old, Similar, and New Responses - MST") +
  xlab("Similarity level") +
  ylab("Proportion") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("New - High Grip" = "#BC3C29FF", "New - Low Grip" = "#ea9999", "Old - High Grip" = "#0072B5FF", "Old - Low Grip" = "#CCE6FF", "Similar - High Grip" = "#20854EFF", "Similar - Low Grip" = "#a7eac6")) +
  scale_linetype_manual(values = c("New - High Grip" = "solid", "New - Low Grip" = "solid", "Old - High Grip" = "solid", "Old - Low Grip" = "solid", "Similar - High Grip" = "solid", "Similar - Low Grip" = "solid")) +
  guides(color = guide_legend(title = "Response Type", ncol = 1), linetype = "none")

5.1.2.2 Continuous

lower_limit <- 0  # Lower limit
upper_limit <- 0.6  # Upper limit

# Filter the data based on the auc_rel_mvc limits
MST_acc_ws <- MST_acc_ws %>%
    filter(auc_rel_mvc <= upper_limit)
MST_acc_ws <- MST_acc_ws %>%
    filter(auc_rel_mvc <= upper_limit)

# Specify the desired order of levels for 'stimLev'
    desired_order <- c("0targ", "1lure", "2lure", "3lure", "4lure", "5foil")

# Convert 'stimLev' to factor with the desired order
MST_acc_ws$stimlev_mst <- factor(MST_acc_ws$stimlev_mst, levels = desired_order)


# Define the plot aesthetics
ggplot(MST_acc_ws, aes(x = stimlev_mst, y = auc_rel_mvc, color = factor(resp1_diff))) +
  
  # Add points for scatter plot
  #geom_point(alpha = 0.7) +
  
  # Set plot title and axis labels
  ggtitle("Relationship between Stimulus Level, Grip Force, and Old/Similar - MST") +
  xlab("Similarity level") +
  ylab("Relative Grip") +
  geom_jitter(alpha = 0.4, width = 0.2, height = 0.05) +

 # Customize legend
  scale_color_manual(values = c("#0072B5FF", "#BC3C29FF"), labels = c("Same (0)", "Different (1)")) +
  scale_size_manual(values = c(3, 6), labels = c("Same (0)", "Different (1)")) +
  guides(color = guide_legend(title = "Response Type"),
         size = guide_legend(title = "Response Type"))

5.1.2.3 Low vs High handgrip for each subject across oddball levels

MST$gf_trPer <- as.factor(MST$gf_trPer)
MST$gf_trPer <- recode_factor(MST$gf_trPer, '0.05' = "Low", '0.4' = "High")
# Calculate individual subject responses
subject_responses_MST_2 <- MST %>%
  group_by(sub, stimlev_mst, gf_trPer) %>%
  summarise(response = mean(resp1_diff, na.rm = TRUE)) %>%
  arrange(gf_trPer) %>%
  ungroup()

# Replace 'sub' with 'gf_trPer' for X-axis labels
subject_responses_MST_2$gf_trPer <- factor(subject_responses_MST_2$gf_trPer, levels = c("Low", "High"))

# Plotting
p10_MST <- ggplot(subject_responses_MST_2, aes(x = gf_trPer, y = response, color=gf_trPer, group = sub, text =   as.character(sub))) +
  geom_line(color = "gray") +
  geom_point(size = 2) +
  ggtitle("Proportion of Responding 'Different' - MST") +
  scale_color_manual(values = c("#0072B5FF", "#BC3C29FF")) +
  xlab("Grip Level") +
  ylab("P('Different')") +
  facet_wrap(~stimlev_mst, scales = "free_x", ncol = length(unique(subject_responses_MST_2$stimlev_mst)))+
  guides(color = guide_legend(title = "Grip Level"))

ggplotly(p10_MST, tooltip = "text")

5.2 Reaction time

5.2.1 Using median within and between subjects

# calculate subject mean
MST_RT_ws <- MST %>%
  group_by(sub, stimlev_mst, resp1) %>%
  mutate(sub_mean = mean(resp1RT, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
MST_RT_ws <- MST_RT_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

MST_RT_ws <- MST_RT_ws %>%
  mutate(RT_diff = sub_mean- grand_mean) 


# Calculate mean RT for old responses
    Old_medRT_MST <- MST_RT_ws %>%
        filter(resp1 == 'old') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(RT= median(resp1RT),RT_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(RT_old = median(RT),
                  RT_old_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
        distinct()
    
# Calculate mean RT for sim responses
    Sim_medRT_MST <- MST_RT_ws %>%
        filter(resp1 == 'sim') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(RT= median(resp1RT),RT_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(RT_sim = median(RT),
                  RT_sim_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
        distinct()
 
# Calculate mean RT for old responses
    New_medRT_MST <- MST_RT_ws %>%
        filter(resp1 == 'new') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(RT= median(resp1RT), RT_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(RT_new = median(RT),
                  RT_new_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
        distinct()
    
# Combine both datasets for plotting
    combined_medRT_MST <- bind_rows(
        mutate(Old_medRT_MST, condition = "Old"),
        mutate(Sim_medRT_MST, condition = "Similar"),
        mutate(New_medRT_MST, condition = "New")
    )
    
# Specify the desired order of levels for 'stimlev_mst'
    desired_order <- c("0targ", "1lure", "2lure", "3lure", "4lure", "5foil")
    
 # Convert 'stimlev_mst' to factor with the desired order
combined_medRT_MST$stimlev_mst <- factor(combined_medRT_MST$stimlev_mst, levels = desired_order)

#Sort the combined dataset by stimlev_mst within each condition
    combined_medRT_MST <- combined_medRT_MST %>%
        arrange(condition, stimlev_mst)
    
# Plotting
ggplot(combined_medRT_MST, aes(x = stimlev_mst)) +
  geom_line(aes(y = RT_old, group = condition, color = "Old"), size = 1.2) +
  geom_line(aes(y = RT_sim, group = condition, color = "Similar"), size = 1.2) +
  geom_line(aes(y = RT_new, group = condition, color = "New"), size = 1.2) +
  geom_point(aes(y = RT_old, color = "Old"), size = 2) +
  geom_point(aes(y = RT_sim, color = "Similar"), size = 2) +
  geom_point(aes(y = RT_new, color = "New"), size = 2) +
  geom_linerange(aes(ymin = RT_old - RT_old_se, ymax = RT_old + RT_old_se), color = "#0072B5FF", size = 0.5) +
  geom_linerange(aes(ymin = RT_sim - RT_sim_se, ymax = RT_sim + RT_sim_se), color = "#20854EFF", size = 0.5) +
  geom_linerange(aes(ymin = RT_new - RT_new_se, ymax = RT_new + RT_new_se), color = "#BC3C29FF", size = 0.5) +
  ggtitle("Median of Median Reaction Time for Old, Similar, and New Responses - MST") +
  xlab("Stimulus Level") +
  ylab("Median Reaction Time") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values=c("Old"="#0072B5FF", "New"="#BC3C29FF", "Similar"="#20854EFF")) +
  guides(color = guide_legend(title = "Response Type"))

5.2.2 Handgrip manipulation

5.2.2.1 Binary

# calculate subject mean
MST_RT_ws <- MST %>%
  group_by(sub, stimlev_mst, resp1) %>%
  mutate(sub_mean = mean(resp1RT, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
MST_RT_ws <- MST_RT_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

MST_RT_ws <- MST_RT_ws %>%
  mutate(RT_diff = sub_mean- grand_mean) 

# Calculate median RT for high - old responses
    Old_RT_high_grip_MST <- MST_RT_ws %>%
      filter(isStrength==1) %>%
      filter(resp1=='old') %>%
      group_by(stimlev_mst, sub) %>%
      summarise(RT= median(resp1RT),RT_diff) %>%
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(RT_old_high = median(RT),
                RT_old_high_se = sd(RT_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()

# Calculate median RT for low - old responses
    Old_RT_low_grip_MST <- MST_RT_ws %>%
      filter(isStrength==0) %>%
      filter(resp1=='old') %>%
      group_by(stimlev_mst, sub) %>%
      summarise(RT= median(resp1RT),RT_diff) %>%
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(RT_old_low = median(RT),
                RT_old_low_se = sd(RT_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()    
    
# Calculate median RT for high-sim responses
    Sim_RT_high_grip_MST <- MST_RT_ws %>%
      filter(isStrength==1) %>%
      filter(resp1=='sim') %>%
      group_by(stimlev_mst, sub) %>%
      summarise(RT= median(resp1RT),RT_diff) %>%
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(RT_sim_high = median(RT),
                RT_sim_high_se = sd(RT_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
    
# Calculate median RT  for low-sim responses
    Sim_RT_low_grip_MST <- MST_RT_ws %>%
      filter(isStrength==0) %>%
      filter(resp1=='sim') %>%
      group_by(stimlev_mst, sub) %>%
      summarise(RT= median(resp1RT),RT_diff) %>%
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(RT_sim_low = median(RT),
                RT_sim_low_se = sd(RT_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
        
# Calculate median RT  for high-new responses
    New_RT_high_grip_MST <- MST_RT_ws %>%
      filter(resp1=='new') %>%
      filter(isStrength==1) %>%
      group_by(stimlev_mst, sub) %>%
      summarise(RT= median(resp1RT),RT_diff) %>%
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(RT_new_high = median(RT),
                RT_new_high_se = sd(RT_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
   
# Calculate median RT for low-new responses
    New_RT_low_grip_MST <- MST_RT_ws %>%
      filter(resp1=='new') %>%
      filter(isStrength==0) %>%
      group_by(stimlev_mst, sub) %>%
      summarise(RT= median(resp1RT),RT_diff) %>%
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(RT_new_low = median(RT),
                RT_new_low_se = sd(RT_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
    
    
# Combine both datasets for plotting
combined_RT_grip_MST <- bind_rows(
  mutate(Old_RT_high_grip_MST, condition = "Old - High Grip", RT = RT_old_high),
  mutate(Old_RT_low_grip_MST, condition = "Old - Low Grip", RT = RT_old_low),
  mutate(Sim_RT_high_grip_MST, condition = "Similar - High Grip", RT = RT_sim_high),
  mutate(Sim_RT_low_grip_MST, condition = "Similar - Low Grip", RT = RT_sim_low),
  mutate(New_RT_high_grip_MST, condition = "New - High Grip", RT = RT_new_high),
  mutate(New_RT_low_grip_MST, condition = "New - Low Grip", RT = RT_new_low)
)

  combined_RT_grip_MST$stimlev_mst <- factor(combined_RT_grip_MST$stimlev_mst, levels = desired_order)

# Specify the desired order of levels for 'stimlev_mst'
    desired_order <- c("0targ", "1lure", "2lure", "3lure", "4lure", "5foil")

# Convert 'stimlev_mst' to factor with the desired order
  combined_RT_grip_MST$stimlev_mst <- factor(combined_RT_grip_MST$stimlev_mst, levels = desired_order)

# Plotting without jitter
ggplot(data = combined_RT_grip_MST) +
  geom_point(aes(x = stimlev_mst, y = RT_old_high, color = condition), size = 2) +
  geom_line(aes(x = stimlev_mst, y = RT_old_high, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimlev_mst, ymin = RT_old_high - RT_old_high_se, 
                     ymax = RT_old_high + RT_old_high_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimlev_mst, y = RT_old_low, color = condition), size = 2) +
  geom_line(aes(x = stimlev_mst, y = RT_old_low, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimlev_mst, ymin = RT_old_low - RT_old_low_se, 
                     ymax = RT_old_low + RT_old_low_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimlev_mst, y = RT_sim_high, color = condition), size = 2) +
  geom_line(aes(x = stimlev_mst, y = RT_sim_high, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimlev_mst, ymin = RT_sim_high - RT_sim_high_se, 
                     ymax = RT_sim_high + RT_sim_high_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimlev_mst, y = RT_sim_low, color = condition), size = 2) +
  geom_line(aes(x = stimlev_mst, y = RT_sim_low, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimlev_mst, ymin = RT_sim_low - RT_sim_low_se, 
                     ymax = RT_sim_low + RT_sim_low_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimlev_mst, y = RT_new_high, color = condition), size = 2) +
  geom_line(aes(x = stimlev_mst, y = RT_new_high, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimlev_mst, ymin = RT_new_high - RT_new_high_se, 
                     ymax = RT_new_high + RT_new_high_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimlev_mst, y = RT_new_low, color = condition), size = 2) +
  geom_line(aes(x = stimlev_mst, y = RT_new_low, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimlev_mst, ymin = RT_new_low - RT_new_low_se, 
                     ymax = RT_new_low + RT_new_low_se, color = condition), 
                 linetype = "solid") +
  
  
  ggtitle("Median of Reaction Time for Same and Different Responses - MST") +
  xlab("Similarity level") +
  ylab("Reaction Time") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("New - High Grip" = "#BC3C29FF", "New - Low Grip" = "#ea9999", "Old - High Grip" = "#0072B5FF", "Old - Low Grip" = "#CCE6FF", "Similar - High Grip" = "#20854EFF", "Similar - Low Grip" = "#a7eac6")) +
  scale_linetype_manual(values = c("New - High Grip" = "solid", "New - Low Grip" = "solid", "Old - High Grip" = "solid", "Old - Low Grip" = "solid", "Similar - High Grip" = "solid", "Similar - Low Grip" = "solid")) +
  guides(color = guide_legend(title = "Response Type", ncol = 1), linetype = "none")

5.3 Confidence

5.3.1 Within-subject SEM

# calculate subject mean
MST_conf_ws <- MST %>%
  group_by(sub, stimlev_mst, resp1_diff) %>%
  mutate(sub_mean = mean(resp2, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
MST_conf_ws <- MST_conf_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

MST_conf_ws <- MST_conf_ws %>%
  mutate(conf_diff = sub_mean- grand_mean)

# Calculate mean confidence for old responses
    Old_conf_MST <- MST_conf_ws %>%
        filter(resp1 == 'old') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(conf_prop= mean(resp2, na.rm = T),conf_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(conf_old = mean(conf_prop, na.rm = T),
                  conf_old_se = sd(conf_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
        distinct()
# Calculate mean confidence for sim responses
    Sim_conf_MST <- MST_conf_ws %>%
        filter(resp1 == 'sim') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(conf_prop= mean(resp2, na.rm = T),conf_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(conf_sim = mean(conf_prop, na.rm = T),
                  conf_sim_se = sd(conf_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
        distinct()
# Calculate mean confidence for new responses
    New_conf_MST <- MST_conf_ws %>%
        filter(resp1 == 'new') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(conf_prop= mean(resp2, na.rm = T),conf_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(conf_new = mean(conf_prop, na.rm = T),
                  conf_new_se = sd(conf_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
        distinct()
    
# Combine both datasets for plotting
    combined_conf_MST <- bind_rows(
        mutate(Old_conf_MST, condition = "Old"),
        mutate(Sim_conf_MST, condition = "Similar"),
        mutate(New_conf_MST, condition = "New")
    )

#####
#diff_average_confidence_MST <- MST %>%
#        filter(resp1_diff== 1) %>%
#        group_by(stimlev_mst, sub) %>%
#        summarise(diff_prop = mean(resp2, na.rm = TRUE)) %>%
#        ungroup() %>%
#        group_by(stimlev_mst) %>%
#        summarise(diff_conf = mean(diff_prop, na.rm = T), 
#                  diff_conf_se = sd(diff_prop, na.rm=T) / sqrt(n_distinct(sub))) %>%
#        distinct()

#  same_average_confidence_MST <- MST %>%
#        filter(resp1_diff== 0) %>%
#        group_by(stimlev_mst, sub) %>%
#        summarise(same_prop = mean(resp2, na.rm = TRUE)) %>%
#        ungroup() %>%
#        group_by(stimlev_mst) %>%
#        summarise(same_conf = mean(same_prop), 
#                  same_conf_se = sd(same_prop) / sqrt(n_distinct(sub))) %>%
#        distinct()

#  combined_prop_MST <- bind_rows(
#        mutate(diff_average_confidence_MST, condition = "Different"),
#        mutate(same_average_confidence_MST, condition = "Same")
#    )

#    # Calculate mean proportion of 'Different' responses for high confidence trials
#    diff_high_confidence_MST <- MST %>%
#        group_by(stimlev_mst) %>%
#        filter(resp2_bin_med == 1) %>%
#        summarise(diff_prop = mean(resp1_diff, na.rm = TRUE),
#                  diff_prop_se = sd(resp1_diff, na.rm = TRUE) / sqrt(length(resp1_diff))) %>%
#        distinct()
#    
#    # Calculate mean proportion of 'Same' responses for high confidence trials
#    same_high_confidence_MST <- diff_high_confidence_MST %>%
#        mutate(not_diff = 1 - diff_prop)
#    
#    # Calculate mean proportion of 'Different' responses for low confidence trials
#    diff_low_confidence_MST <- MST %>%
#        group_by(stimlev_mst) %>%
#        filter(resp2_bin_med == 0) %>%
#        summarise(diff_prop = mean(resp1_diff, na.rm = TRUE),
#                  diff_prop_se = sd(resp1_diff, na.rm = TRUE) / sqrt(length(resp1_diff))) %>%
#        distinct()
    
#    # Calculate mean proportion of 'Same' responses for low confidence trials
#    same_low_confidence_MST <- diff_low_confidence_MST %>%
#        mutate(not_diff = 1 - diff_prop)
    
#    # Combine both datasets for plotting
#    combined_prop_MST <- bind_rows(
#        mutate(diff_high_confidence_MST, condition = "High Confidence - Different", prop = diff_prop),
#        mutate(same_high_confidence_MST, condition = "High Confidence - Same", prop = not_diff),
#        mutate(diff_low_confidence_MST, condition = "Low Confidence - Different", prop = diff_prop),
#        mutate(same_low_confidence_MST, condition = "Low Confidence - Same", prop = not_diff)
#    )
#####    
    
    # Specify the desired order of levels for 'stimlev_mst'
    desired_order <- c("0targ", "1lure", "2lure", "3lure", "4lure", "5foil")
    
    # Convert 'stimlev_mst' to factor with the desired order
    combined_conf_MST$stimlev_mst <- factor(combined_conf_MST$stimlev_mst, levels = desired_order)
    
    # Sort the combined dataset by stimlev_mst within each condition
    combined_conf_MST <- combined_conf_MST %>%
        arrange(condition, stimlev_mst)
    
    # Plotting
    ggplot(combined_conf_MST, aes(x = stimlev_mst)) +
  geom_line(aes(y = conf_old, group = condition, color = "Old"), size = 1.2) +
  geom_line(aes(y = conf_sim, group = condition, color = "Similar"), size = 1.2) +
  geom_line(aes(y = conf_new, group = condition, color = "New"), size = 1.2) +
  geom_point(aes(y = conf_old, color = "Old"), size = 2) +
  geom_point(aes(y = conf_sim, color = "Similar"), size = 2) +
  geom_point(aes(y = conf_new, color = "New"), size = 2) +
  geom_linerange(aes(ymin = conf_old - conf_old_se, ymax = conf_old + conf_old_se), color = "#0072B5FF", size = 0.5) +
  geom_linerange(aes(ymin = conf_sim - conf_sim_se, ymax = conf_sim + conf_sim_se), color = "#20854EFF", size = 0.5) +
  geom_linerange(aes(ymin = conf_new - conf_new_se, ymax = conf_new + conf_new_se), color = "#BC3C29FF", size = 0.5) +
  ggtitle("Average of Confidence for Old, Similar, and New Responses - MST") +
  xlab("Stimulus Level") +
  ylab("Confidence Rating") +
  ylim(2,4)+
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values=c("Old"="#0072B5FF", "New"="#BC3C29FF", "Similar"="#20854EFF")) +
  guides(color = guide_legend(title = "Response Type"))

5.3.2 Handgrip manipulation

5.3.2.1 Binary

# calculate subject mean
MST_conf_ws <- MST %>%
  group_by(sub, stimlev_mst, resp1_diff) %>%
  mutate(sub_mean = mean(resp2, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
MST_conf_ws <- MST_conf_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

MST_conf_ws <- MST_conf_ws %>%
  mutate(conf_diff = sub_mean- grand_mean)

# Calculate mean confidence for High old responses
    Old_conf_high_grip_MST <- MST_conf_ws %>%
        filter(isStrength==1) %>%
        filter(resp1 == 'old') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(conf_prop= mean(resp2, na.rm = T),conf_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(conf_high_old = mean(conf_prop, na.rm = T),
                  conf_high_old_se = sd(conf_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
        distinct()
    
# Calculate mean confidence for low old responses
    Old_conf_low_grip_MST <- MST_conf_ws %>%
        filter(isStrength==0) %>%
        filter(resp1 == 'old') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(conf_prop= mean(resp2, na.rm = T),conf_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(conf_low_old = mean(conf_prop, na.rm = T),
                  conf_low_old_se = sd(conf_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
        distinct()
    
# Calculate mean confidence for High sim responses
    Sim_conf_high_grip_MST <- MST_conf_ws %>%
        filter(isStrength==1) %>%
        filter(resp1 == 'sim') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(conf_prop= mean(resp2, na.rm = T),conf_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(conf_high_sim = mean(conf_prop, na.rm = T),
                  conf_high_sim_se = sd(conf_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
        distinct()
    
# Calculate mean confidence for Low sim responses
    Sim_conf_low_grip_MST <- MST_conf_ws %>%
        filter(isStrength==0) %>%
        filter(resp1 == 'sim') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(conf_prop= mean(resp2, na.rm = T),conf_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(conf_low_sim = mean(conf_prop, na.rm = T),
                  conf_low_sim_se = sd(conf_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
        distinct()
    
# Calculate mean confidence for High new responses
    New_conf_high_grip_MST <- MST_conf_ws %>%
        filter(isStrength ==1) %>%
        filter(resp1 == 'new') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(conf_prop= mean(resp2, na.rm = T),conf_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(conf_high_new = mean(conf_prop, na.rm = T),
                  conf_high_new_se = sd(conf_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
        distinct()
# Calculate mean confidence for Low new responses
    New_conf_low_grip_MST <- MST_conf_ws %>%
        filter(isStrength ==0) %>%
        filter(resp1 == 'new') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(conf_prop= mean(resp2, na.rm = T),conf_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(conf_low_new = mean(conf_prop, na.rm = T),
                  conf_low_new_se = sd(conf_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
        distinct()    
    
# Combine both datasets for plotting
    combined_conf_grip_MST <- bind_rows(
        mutate(Old_conf_high_grip_MST, condition = "Old - High Grip"),
        mutate(Sim_conf_high_grip_MST, condition = "Similar - High Grip"),
        mutate(New_conf_high_grip_MST, condition = "New - High Grip"),
        mutate(Old_conf_low_grip_MST, condition = "Old - Low Grip"),
        mutate(Sim_conf_low_grip_MST, condition = "Similar - Low Grip"),
        mutate(New_conf_low_grip_MST, condition = "New - Low Grip")
    )

    
    # Specify the desired order of levels for 'stimlev_mst'
    desired_order <- c("0targ", "1lure", "2lure", "3lure", "4lure", "5foil")
    
    # Convert 'stimlev_mst' to factor with the desired order
    combined_conf_grip_MST$stimlev_mst <- factor(combined_conf_grip_MST$stimlev_mst, levels = desired_order)
    
    # Sort the combined dataset by stimlev_mst within each condition
    combined_conf_grip_MST <- combined_conf_grip_MST %>%
        arrange(condition, stimlev_mst)
    
ggplot(combined_conf_grip_MST, aes(x = stimlev_mst)) +
  geom_line(aes(y = conf_high_old, group = condition, color = condition), size = 1.2) +
  geom_point(aes(y = conf_high_old, color = condition), size = 2) +
  geom_linerange(aes(ymin = conf_high_old - conf_high_old_se, ymax = conf_high_old + conf_high_old_se, color = condition), size = 0.5) +
  geom_line(aes(y = conf_high_sim, group = condition, color = condition), size = 1.2, linetype = "solid") +
  geom_point(aes(y = conf_high_sim, color = condition), size = 2) +
  geom_linerange(aes(ymin = conf_high_sim - conf_high_sim_se, ymax = conf_high_sim + conf_high_sim_se, color = condition), size = 0.5) +
  geom_line(aes(y = conf_high_new, group = condition, color = condition), size = 1.2, linetype = "solid") +
  geom_point(aes(y = conf_high_new, color = condition), size = 2) +
  geom_linerange(aes(ymin = conf_high_new - conf_high_new_se, ymax = conf_high_new + conf_high_new_se, color = condition), size = 0.5) +
  geom_line(aes(y = conf_low_old, group = condition, color = condition), size = 1.2) +
  geom_point(aes(y = conf_low_old, color = condition), size = 2) +
  geom_linerange(aes(ymin = conf_low_old - conf_low_old_se, ymax = conf_low_old + conf_low_old_se, color = condition), size = 0.5) +
  geom_line(aes(y = conf_low_sim, group = condition, color = condition), size = 1.2, linetype = "solid") +
  geom_point(aes(y = conf_low_sim, color = condition), size = 2) +
  geom_linerange(aes(ymin = conf_low_sim - conf_low_sim_se, ymax = conf_low_sim + conf_low_sim_se, color = condition), size = 0.5) +
  geom_line(aes(y = conf_low_new, group = condition, color = condition), size = 1.2, linetype = "solid") +
  geom_point(aes(y = conf_low_new, color = condition), size = 2) +
  geom_linerange(aes(ymin = conf_low_new - conf_low_new_se, ymax = conf_low_new + conf_low_new_se, color = condition), size = 0.5) +
  ggtitle("Average of Confidence for Old, Similar, and New Responses - MST") +
  xlab("Stimulus Level") +
  ylab("Confidence Rating") +
  ylim(2, 4) +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("#0072B5FF", "#CCE6FF", "#20854EFF", "#a7eac6", "#BC3C29FF", "#ea9999"),
                     name = "Condition",
                     labels = c("Old - High Grip", "Old - Low Grip", "Similar - High Grip", "Similar - Low Grip", "New - High Grip", "New - Low Grip")) +
  scale_linetype_manual(values = c("solid", "solid", "solid", "solid", "solid", "solid"), 
                        name = "Condition",
                        labels = c("Old - High Grip", "Old - Low Grip", "Similar - High Grip", "Similar - Low Grip", "New - High Grip", "New - Low Grip")) +
  guides(color = guide_legend(title = "Response Type"))

5.4 other MST metrics

5.4.1 REC

Corrected Recognition Memory \[ REC=P(Old|Target) - P(Old|Foil) \]

# Calculate the probability of responding "old" given the trial type is "target"
prob_old_target <- MST %>%
  group_by(sub) %>%
  filter(trial_type == 'targ') %>%
  summarise(prob_old_given_target = sum(resp1 == 'old') / n()) %>%
  ungroup()

# Calculate the probability of responding "old" given the trial type is "foil"
prob_old_foil <- MST %>%
    group_by(sub) %>%
  filter(trial_type == 'foil') %>%
  summarise(prob_old_given_foil = sum(resp1 == 'old') / n()) %>%
  ungroup()


# Merge prob_old_target and prob_old_foil data frames
MST_REC <- merge(prob_old_target, prob_old_foil, by = "sub", suffixes = c("_target", "_foil"))

# Calculate REC (Remembered Old - Forgotten Old)
MST_REC$REC <- MST_REC$prob_old_given_target - MST_REC$prob_old_given_foil

5.4.2 LDI

Lure Discrimination Index \[ LDI=P(Similar|Lure) - P(Similar|Foil) \]

# Calculate the probability of responding "sim" given the trial type is "lure"
prob_sim_lure <- MST %>%
  group_by(sub) %>%
  filter(trial_type == 'lure') %>%
  summarise(prob_sim_given_lure = sum(resp1 == 'sim') / n()) %>%
  ungroup()

# Calculate the probability of responding "sim" given the trial type is "foil"
prob_sim_foil <- MST %>%
  group_by(sub) %>%
  filter(trial_type == 'foil') %>%
  summarise(prob_sim_given_foil = sum(resp1 == 'sim') / n()) %>%
  ungroup()

# Merge prob_sim_lure and prob_sim_foil data frames
MST_LDI <- merge(prob_sim_lure, prob_sim_foil, by = "sub", suffixes = c("_lure", "_foil"))

# Calculate LDI (List Discrimination Index)
MST_LDI$LDI <- MST_LDI$prob_sim_given_lure - MST_LDI$prob_sim_given_foil


# Merge MST_REC and MST_LDI data frames
MST_combined <- merge(MST_REC, MST_LDI, by = "sub")

# Plotting
ggplot(MST_combined, aes(x = REC, y = LDI)) +
  geom_point() +
  ggtitle("LDI vs REC for Each Subject") +
  xlab("Correct Recognition Memory") +
  ylab("Lure Discrimination Index")

6 Analysis of subjects completed 4 tasks

6.1 List of their subject ID

sub_with_all_tasks <- combined_data %>%
  group_by(sub) %>%
  filter(all(c("ADT", "VDT", "CDT", "MST") %in% task)) %>%
  pull(sub) %>%
  unique()

print(sub_with_all_tasks)
##  [1] 13 16 17 18 19 23 24 25 26 27 29 31 33 34 35 36 37 42 43 46 48 54 56 58 61
## [26] 64 65 68 69 71 72 74 82 83 85 87 88

6.2 ADT

6.2.1 Specific Dataframes

ADT_4T <- lcya_behdata_trial_avdt %>%
  filter(task=='aud') %>%
  filter(!is.na(resp1)) %>%
  filter(sub %in% c('13','16','17','18','19','23','24','25','26','27','29','31','33','34','35','36','37','42','43','46','48','54','56','58','61','64','65','68','69','71','72','74','82','83','85','87','88'))

6.2.1.1 Proportion of Responding ‘Different’

# calculate subject mean
ADT_4T_acc_ws <- ADT_4T %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1_diff, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
ADT_4T_acc_ws <- ADT_4T_acc_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

ADT_4T_acc_ws <- ADT_4T_acc_ws %>%
  mutate(acc_diff = sub_mean- grand_mean) 

# Calculate mean accuracy and standard error for 'Different'
Diff_prop_ADT_4T <- ADT_4T_acc_ws %>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(diff=mean(diff_sub), diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate the proportion of 'Same'
Same_prop_ADT_4T <- ADT_4T_acc_ws %>%
    group_by(sub, stimLev) %>%
    summarise(same_sub = 1-mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(not_diff=mean(same_sub), not_diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

# Combine both datasets for plotting
combined_ADT_4T <- bind_rows(
  mutate(Diff_prop_ADT_4T, condition = "Different", prop = diff),
  mutate(Same_prop_ADT_4T, condition = "Same", prop = not_diff)
)

# Specify the desired order of levels for 'stimLev'
desired_order <- c("0", "4", "8", "32", "128")

# Convert 'stimLev' to factor with the desired order
combined_ADT_4T$stimLev <- factor(combined_ADT_4T$stimLev, levels = desired_order)

# Plotting with the modified 'stimLev'
ggplot(combined_ADT_4T, aes(x = stimLev, y = prop, group = condition, color = condition)) +
  geom_point(size = 2) +
  geom_line(size = 1.2) +
  geom_linerange(mapping = aes(ymin = prop - ifelse(condition == "Different", diff_se, not_diff_se), 
                                ymax = prop + ifelse(condition == "Different", diff_se, not_diff_se))) +
  ylim(0, 1) +
  ggtitle("Proportion of Responding 'Different' and 'Same' - ADT'") +
  xlab("Oddball Level (Hz)") +
  ylab("Proportion") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("#BC3C29FF", "#0072B5FF")) +
  guides(color = guide_legend(title = "condition"))

6.2.1.2 Handgrip manipulation

# calculate subject mean
ADT_4T_acc_ws <- ADT_4T %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1_diff, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
ADT_4T_acc_ws <- ADT_4T_acc_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

ADT_4T_acc_ws <- ADT_4T_acc_ws %>%
  mutate(acc_diff = sub_mean- grand_mean) 

# Calculate mean accuracy and standard error for 'Different'
Diff_prop_high_grip_ADT_4T <- ADT_4T_acc_ws %>%
    filter(isStrength==1)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(diff=mean(diff_sub), diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

Diff_prop_low_grip_ADT_4T <- ADT_4T_acc_ws %>%
    filter(isStrength==0)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(diff=mean(diff_sub), diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate the proportion of 'Same'
Same_prop_high_grip_ADT_4T <- ADT_4T_acc_ws %>%
    filter(isStrength==1) %>%
    group_by(sub, stimLev) %>%
    summarise(same_sub = 1-mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(not_diff=mean(same_sub), not_diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

Same_prop_low_grip_ADT_4T <- ADT_4T_acc_ws %>%
    filter(isStrength==0) %>%
    group_by(sub, stimLev) %>%
    summarise(same_sub = 1-mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(not_diff=mean(same_sub), not_diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

# Combine both datasets for plotting
combined_grip_ADT_4T <- bind_rows(
  mutate(Diff_prop_high_grip_ADT_4T, condition = "Different - High Grip", prop = diff),
  mutate(Diff_prop_low_grip_ADT_4T, condition = "Different - Low Grip", prop = diff),
  mutate(Same_prop_high_grip_ADT_4T, condition = "Same - High Grip", prop = not_diff),
  mutate(Same_prop_low_grip_ADT_4T, condition = "Same - Low Grip", prop = not_diff)
)

# Specify the desired order of levels for 'stimLev'
desired_order <- c("0", "4", "8", "32", "128")

# Convert 'stimLev' to factor with the desired order
combined_grip_ADT_4T$stimLev <- factor(combined_grip_ADT_4T$stimLev, levels = desired_order)

# Plotting
ggplot(combined_grip_ADT_4T, aes(x = stimLev, y = prop, group = condition, color = condition)) +
  geom_point( size = 2) +
  geom_line(size = 1.2) +
  geom_linerange(mapping = aes(ymin = prop - ifelse(condition == "Different - High Grip" | condition == "Different - Low Grip", diff_se, not_diff_se), 
                               ymax = prop + ifelse(condition == "Different - High Grip" | condition == "Different - Low Grip", diff_se, not_diff_se)), show.legend = FALSE) +
  ylim(0, 1) +
  ggtitle("Proportion of Responding 'Different' and 'Same' - ADT'") +
  xlab("Oddball Level (Hz)") +
  ylab("Proportion") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("Different - High Grip" = "#BC3C29FF", "Different - Low Grip" = "#ea9999", "Same - High Grip" = "#0072B5FF", "Same - Low Grip" = "#CCE6FF")) +
  scale_linetype_manual(values = c("Different - High Grip" = "solid", "Different - Low Grip" = "solid", "Same - High Grip" = "solid", "Same - Low Grip" = "solid")) +
  guides(color = guide_legend(title = "Response Type", ncol = 1), linetype = "none")

6.2.2 Reaction Time

6.2.2.1 Using Median with within-subject SEM

# calculate subject mean
ADT_4T_RT_ws <- ADT_4T %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1RT, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calcualte the grand mean
ADT_4T_RT_ws <- ADT_4T_RT_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

ADT_4T_RT_ws <- ADT_4T_RT_ws %>%
  mutate(RT_diff = sub_mean- grand_mean) 


# Calculate median RT for different responses
ADT_4T_diff_RT_ws <- ADT_4T_RT_ws %>%
  filter(resp1_diff == 1) %>%
  group_by(stimLev,sub) %>%
  summarise(diff_sub= median(resp1RT), RT_diff) %>%
  ungroup() %>%
  group_by(stimLev) %>%
  summarise(medRT_diff = median(diff_sub),
            medRT_diff_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
  distinct()

# Calculate median RT for same responses
ADT_4T_same_RT_ws <- ADT_4T_RT_ws %>%
  filter(resp1_diff == 0) %>%
  group_by(stimLev,sub) %>%
  summarise(diff_sub= median(resp1RT), RT_diff) %>%
  ungroup() %>%
  group_by(stimLev) %>%
  summarise(medRT_same = median(diff_sub),
            medRT_same_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
  distinct()

# Combine both datasets for plotting
combined_medRT_ADT_4T <- bind_rows(
  mutate(ADT_4T_diff_RT_ws, condition = "Different"),
  mutate(ADT_4T_same_RT_ws, condition = "Same")
)


# Specify the desired order of levels for 'stimLev'
desired_order <- c("0", "4", "8", "32", "128")

# Convert 'stimLev' to factor with the desired order
combined_medRT_ADT_4T$stimLev <- factor(combined_medRT_ADT_4T$stimLev, levels = desired_order)

#Sort the combined dataset by stimLev within each condition
combined_medRT_ADT_4T <- combined_medRT_ADT_4T %>%
  arrange(condition, stimLev)

# Plotting
ggplot(combined_medRT_ADT_4T, aes(x = stimLev, group = condition, color = condition, linetype= condition)) +
  geom_point(aes(y = ifelse(condition == "Different", medRT_diff , medRT_same)), size = 2) +
  geom_line(aes(y = ifelse(condition == "Different", medRT_diff, medRT_same)), size = 1.2) +
  ylim(min(c(combined_medRT_ADT_4T$medRT_diff, combined_medRT_ADT_4T$medRT_same)), max(c(combined_medRT_ADT_4T$medRT_diff, combined_medRT_ADT_4T$medRT_same))) +
  ggtitle("Median of Reaction Time for Same and Different Responses - ADT") +
  geom_linerange(mapping = aes(ymin = medRT_diff - medRT_diff_se, ymax = medRT_diff + medRT_diff_se, linetype = condition), data = filter(combined_medRT_ADT_4T, condition == "Different")) +
  geom_linerange(mapping = aes(ymin = medRT_same - medRT_same_se, ymax = medRT_same + medRT_same_se, linetype = condition), data = filter(combined_medRT_ADT_4T, condition == "Same")) +
  labs(caption = "Error bars represent within-subject SEM") +
  xlab("Oddball Level (Hz)") +
  ylab("Reaction Time (S)") +
  scale_color_manual(values = c( "#BC3C29FF", "#0072B5FF")) +
  scale_linetype_manual(values = c("Different" = "solid", "Same" = "solid")) 

6.2.2.2 Using Median within and Mean between subjects with WSSEM

# calculate subject mean
ADT_4T_RT_ws <- ADT_4T %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1RT, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calcualte the grand mean
ADT_4T_RT_ws <- ADT_4T_RT_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

ADT_4T_RT_ws <- ADT_4T_RT_ws %>%
  mutate(RT_diff = sub_mean- grand_mean) 


# Calculate median RT for different responses
ADT_4T_diff_RT_ws <- ADT_4T_RT_ws %>%
  filter(resp1_diff == 1) %>%
  group_by(stimLev,sub) %>%
  summarise(diff_sub= median(resp1RT), RT_diff) %>%
  ungroup() %>%
  group_by(stimLev) %>%
  summarise(medRT_diff = mean(diff_sub, na.rm = T),
            medRT_diff_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
  distinct()

# Calculate median RT for same responses
ADT_4T_same_RT_ws <- ADT_4T_RT_ws %>%
  filter(resp1_diff == 0) %>%
  group_by(stimLev,sub) %>%
  summarise(diff_sub= median(resp1RT), RT_diff) %>%
  ungroup() %>%
  group_by(stimLev) %>%
  summarise(medRT_same = mean(diff_sub, na.rm = T),
            medRT_same_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
  distinct()

# Combine both datasets for plotting
combined_RT_ADT_4T <- bind_rows(
  mutate(ADT_4T_diff_RT_ws, condition = "Different"),
  mutate(ADT_4T_same_RT_ws, condition = "Same")
)


# Specify the desired order of levels for 'stimLev'
desired_order <- c("0", "4", "8", "32", "128")

# Convert 'stimLev' to factor with the desired order
combined_RT_ADT_4T$stimLev <- factor(combined_RT_ADT_4T$stimLev, levels = desired_order)

#Sort the combined dataset by stimLev within each condition
combined_RT_ADT_4T <- combined_RT_ADT_4T %>%
  arrange(condition, stimLev)

# Plotting
ggplot(combined_RT_ADT_4T, aes(x = stimLev, group = condition, color = condition, linetype= condition)) +
  geom_point(aes(y = ifelse(condition == "Different", medRT_diff , medRT_same)), size = 2) +
  geom_line(aes(y = ifelse(condition == "Different", medRT_diff, medRT_same)), size = 1.2) +
  ylim(min(c(combined_RT_ADT_4T$medRT_diff, combined_RT_ADT_4T$medRT_same)), max(c(combined_RT_ADT_4T$medRT_diff, combined_RT_ADT_4T$medRT_same))) +
  ggtitle("Median of Reaction Time within and Mean between - ADT") +
  geom_linerange(mapping = aes(ymin = medRT_diff - medRT_diff_se, ymax = medRT_diff + medRT_diff_se, linetype = condition), data = filter(combined_RT_ADT_4T, condition == "Different")) +
  geom_linerange(mapping = aes(ymin = medRT_same - medRT_same_se, ymax = medRT_same + medRT_same_se, linetype = condition), data = filter(combined_RT_ADT_4T, condition == "Same")) +
  labs(caption = "Error bars represent within-subject SEM") +
  xlab("Oddball Level (Hz)") +
  ylab("Reaction Time (S)") +
  scale_color_manual(values = c( "#BC3C29FF", "#0072B5FF")) +
  scale_linetype_manual(values = c("Different" = "solid", "Same" = "solid")) 

6.2.2.3 Handgrip manipulation with Median

# calculate subject mean
ADT_4T_RT_ws <- ADT_4T %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1RT, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calcualte the grand mean
ADT_4T_RT_ws <- ADT_4T_RT_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

ADT_4T_RT_ws <- ADT_4T_RT_ws %>%
  mutate(RT_diff = sub_mean- grand_mean) 

# Calculate mean RT and standard error for  High 'Different'
RT_diff_high_grip_ADT_4T <- ADT_4T_RT_ws %>%
    filter(resp1_diff == 1) %>%
    filter(isStrength==1)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_high_diff=median(diff_sub), medRT_high_diff_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate mean RT and standard error for  Low 'Different'
RT_diff_low_grip_ADT_4T <- ADT_4T_RT_ws %>%
    filter(resp1_diff == 1) %>%
    filter(isStrength==0)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_low_diff=median(diff_sub), medRT_low_diff_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()


# Calculate mean RT and standard error for  High 'Same'
RT_same_high_grip_ADT_4T <- ADT_4T_RT_ws %>%
    filter(resp1_diff == 0) %>%
    filter(isStrength==1)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_high_same=median(diff_sub), medRT_high_same_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate mean RT and standard error for  Low 'Same'
RT_same_low_grip_ADT_4T <- ADT_4T_RT_ws %>%
    filter(resp1_diff == 0) %>%
    filter(isStrength==0)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_low_same=median(diff_sub), medRT_low_same_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()


# Combine both datasets for plotting
combined_medRT_grip_ADT_4T <- bind_rows(
  mutate(RT_diff_high_grip_ADT_4T, condition = "Different - High Grip"),
  mutate(RT_diff_low_grip_ADT_4T, condition = "Different - Low Grip"),
  mutate(RT_same_high_grip_ADT_4T, condition = "Same - High Grip"),
  mutate(RT_same_low_grip_ADT_4T, condition = "Same - Low Grip")
)

# Specify the desired order of levels for 'stimLev'
desired_order <- c("0", "4", "8", "32", "128")

# Convert 'stimLev' to factor with the desired order
combined_medRT_grip_ADT_4T$stimLev <- factor(combined_medRT_grip_ADT_4T$stimLev, levels = desired_order)


# Plotting
ggplot(data = combined_medRT_grip_ADT_4T) +
  geom_point(aes(x = stimLev, y = medRT_high_diff, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_high_diff, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_high_diff - medRT_high_diff_se, 
                     ymax = medRT_high_diff + medRT_high_diff_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_low_diff, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_low_diff, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_low_diff - medRT_low_diff_se, 
                     ymax = medRT_low_diff + medRT_low_diff_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_high_same, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_high_same, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_high_same - medRT_high_same_se, 
                     ymax = medRT_high_same + medRT_high_same_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_low_same, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_low_same, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_low_same - medRT_low_same_se, 
                     ymax = medRT_low_same + medRT_low_same_se, color = condition), 
                 linetype = "solid") +
  
  ggtitle("Median of Reaction Time for Same and Different Responses - ADT") +
  xlab("Oddball Level (Hz)") +
  ylab("Reaction Time") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("#BC3C29FF", "#ea9999", "#0072B5FF", "#CCE6FF"),
                      name = "Condition") 

6.2.2.4 Handgrip manipulation with Median within and Mean between with WSSEM

# calculate subject mean
ADT_4T_RT_ws <- ADT_4T %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1RT, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calcualte the grand mean
ADT_4T_RT_ws <- ADT_4T_RT_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

ADT_4T_RT_ws <- ADT_4T_RT_ws %>%
  mutate(RT_diff = sub_mean- grand_mean) 

# Calculate mean RT and standard error for  High 'Different'
RT_diff_high_grip_ADT_4T <- ADT_4T_RT_ws %>%
    filter(resp1_diff == 1) %>%
    filter(isStrength==1)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_high_diff=mean(diff_sub, na.rm = T), medRT_high_diff_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate mean RT and standard error for  Low 'Different'
RT_diff_low_grip_ADT_4T <- ADT_4T_RT_ws %>%
    filter(resp1_diff == 1) %>%
    filter(isStrength==0)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_low_diff=mean(diff_sub, na.rm = T), medRT_low_diff_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()


# Calculate mean RT and standard error for  High 'Same'
RT_same_high_grip_ADT_4T <- ADT_4T_RT_ws %>%
    filter(resp1_diff == 0) %>%
    filter(isStrength==1)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_high_same=mean(diff_sub, na.rm = T), medRT_high_same_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate mean RT and standard error for  Low 'Same'
RT_same_low_grip_ADT_4T <- ADT_4T_RT_ws %>%
    filter(resp1_diff == 0) %>%
    filter(isStrength==0)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_low_same=mean(diff_sub, na.rm = T), medRT_low_same_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()


# Combine both datasets for plotting
combined_RT_grip_ADT_4T <- bind_rows(
  mutate(RT_diff_high_grip_ADT_4T, condition = "Different - High Grip"),
  mutate(RT_diff_low_grip_ADT_4T, condition = "Different - Low Grip"),
  mutate(RT_same_high_grip_ADT_4T, condition = "Same - High Grip"),
  mutate(RT_same_low_grip_ADT_4T, condition = "Same - Low Grip")
)

# Specify the desired order of levels for 'stimLev'
desired_order <- c("0", "4", "8", "32", "128")

# Convert 'stimLev' to factor with the desired order
combined_RT_grip_ADT_4T$stimLev <- factor(combined_RT_grip_ADT_4T$stimLev, levels = desired_order)


# Plotting
ggplot(data = combined_RT_grip_ADT_4T) +
  geom_point(aes(x = stimLev, y = medRT_high_diff, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_high_diff, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_high_diff - medRT_high_diff_se, 
                     ymax = medRT_high_diff + medRT_high_diff_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_low_diff, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_low_diff, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_low_diff - medRT_low_diff_se, 
                     ymax = medRT_low_diff + medRT_low_diff_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_high_same, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_high_same, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_high_same - medRT_high_same_se, 
                     ymax = medRT_high_same + medRT_high_same_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_low_same, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_low_same, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_low_same - medRT_low_same_se, 
                     ymax = medRT_low_same + medRT_low_same_se, color = condition), 
                 linetype = "solid") +
  
  ggtitle("Median of Reaction Time Within and Mean Between - ADT") +
  xlab("Oddball Level (Hz)") +
  ylab("Reaction Time") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("#BC3C29FF", "#ea9999", "#0072B5FF", "#CCE6FF"),
                      name = "Condition") 

6.2.3 Confidence

# calculate subject mean
ADT_4T_conf_ws <- ADT_4T %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp2, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
ADT_4T_conf_ws <- ADT_4T_conf_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

ADT_4T_conf_ws <- ADT_4T_conf_ws %>%
  mutate(conf_diff = sub_mean- grand_mean) 

diff_average_confidence_ADT_4T <- ADT_4T_conf_ws %>%
        filter(resp1_diff== 1) %>%
        group_by(stimLev, sub) %>%
        summarise(diff_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(diff_conf = mean(diff_prop, na.rm = T), 
                  diff_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()

same_average_confidence_ADT_4T <- ADT_4T_conf_ws %>%
        filter(resp1_diff== 0) %>%
        group_by(stimLev, sub) %>%
        summarise(same_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(same_conf = mean(same_prop), 
                  same_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()

  combined_prop_ADT_4T <- bind_rows(
        mutate(diff_average_confidence_ADT_4T, condition = "Different"),
        mutate(same_average_confidence_ADT_4T, condition = "Same")
    )
  
    # Specify the desired order of levels for 'stimLev'
    desired_order <- c("0", "4", "8", "32", "128")
    
    # Convert 'stimLev' to factor with the desired order
    combined_prop_ADT_4T$stimLev <- factor(combined_prop_ADT_4T$stimLev, levels = desired_order)
    
    # Sort the combined dataset by stimLev within each condition
    combined_prop_ADT_4T <- combined_prop_ADT_4T %>%
        arrange(condition, stimLev)
    
    # Plotting
    ggplot(combined_prop_ADT_4T, aes(x = stimLev, group = condition, color = condition)) +
        geom_point(aes(y = ifelse(condition == "Different", diff_conf, same_conf)), size = 2) +
    geom_line(aes(y = ifelse(condition == "Different", diff_conf, same_conf)), size = 1.2) +
        ylim(2, 4) +
        ggtitle("Average of Confidence Response - ADT") +
        geom_linerange(mapping = aes(ymin = diff_conf - diff_conf_se, ymax = diff_conf + diff_conf_se, linetype = condition), data = filter(combined_prop_ADT_4T, condition == "Different")) +
    geom_linerange(mapping = aes(ymin = same_conf - same_conf_se, ymax = same_conf + same_conf_se, linetype = condition), data = filter(combined_prop_ADT_4T, condition == "Same")) +
        labs(caption = "Error bars represent within-subject SEM") +
        xlab("Oddball Level") +
        ylab("Confidence Rating") +
        scale_color_manual(values = c("Different" = "#BC3C29FF",
                                      "Same" = "#0072B5FF"
                                      ),
                           labels = c("Different",
                                      "Same"
                                      )) +
        scale_linetype_manual(values = c("Different" = "solid", "Same" = "solid"))

6.2.3.1 Handgrip manipulation

# calculate subject mean
ADT_4T_conf_ws <- ADT_4T %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp2, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
ADT_4T_conf_ws <- ADT_4T_conf_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

ADT_4T_conf_ws <- ADT_4T_conf_ws %>%
  mutate(conf_diff = sub_mean- grand_mean) 

diff_average_high_confidence_ADT_4T <- ADT_4T_conf_ws %>%
        filter(isStrength == 1) %>%
        filter(resp1_diff== 1) %>%
        group_by(stimLev, sub) %>%
        summarise(diff_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(diff_high_conf = mean(diff_prop, na.rm = T), 
                  diff_high_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()

diff_average_low_confidence_ADT_4T <- ADT_4T_conf_ws %>%
        filter(isStrength == 0) %>%
        filter(resp1_diff== 1) %>%
        group_by(stimLev, sub) %>%
        summarise(diff_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(diff_low_conf = mean(diff_prop, na.rm = T), 
                  diff_low_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()


same_average_high_confidence_ADT_4T <- ADT_4T_conf_ws %>%
        filter(isStrength == 1) %>%
        filter(resp1_diff== 0) %>%
        group_by(stimLev, sub) %>%
        summarise(same_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(same_high_conf = mean(same_prop, na.rm = T), 
                  same_high_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()

same_average_low_confidence_ADT_4T <- ADT_4T_conf_ws %>%
        filter(isStrength == 0) %>%
        filter(resp1_diff== 0) %>%
        group_by(stimLev, sub) %>%
        summarise(same_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(same_low_conf = mean(same_prop, na.rm = T), 
                  same_low_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()


combined_conf_grip_ADT_4T <- bind_rows(
        mutate(diff_average_high_confidence_ADT_4T, condition = "Different - High Grip"),
        mutate(diff_average_low_confidence_ADT_4T, condition = "Different - Low Grip" ),
        mutate(same_average_high_confidence_ADT_4T, condition = "Same - High Grip"),
        mutate(same_average_low_confidence_ADT_4T, condition = "Same - Low Grip")
)
  
    # Specify the desired order of levels for 'stimLev'
    desired_order <- c("0", "4", "8", "32", "128")
    
    # Convert 'stimLev' to factor with the desired order
    combined_conf_grip_ADT_4T$stimLev <- factor(combined_conf_grip_ADT_4T$stimLev, levels = desired_order)
    
    # Sort the combined dataset by stimLev within each condition
    combined_conf_grip_ADT_4T <- combined_conf_grip_ADT_4T %>%
        arrange(condition, stimLev)
    


ggplot(combined_conf_grip_ADT_4T, aes(x = stimLev, color = condition)) +
  geom_point(aes(y = diff_high_conf), size = 3) +
  geom_line(aes(y = diff_high_conf, group = condition), size = 1.2) +
  geom_linerange(aes(ymin = diff_high_conf - diff_high_conf_se, 
                     ymax = diff_high_conf + diff_high_conf_se, group = condition), 
                 width = 0.2, linetype = "solid") +
  geom_point(aes(y = diff_low_conf), size = 3) +
  geom_line(aes(y = diff_low_conf, group = condition), size = 1.2, linetype = "solid") +
  geom_linerange(aes(ymin = diff_low_conf - diff_low_conf_se, 
                     ymax = diff_low_conf + diff_low_conf_se, group = condition), 
                 width = 0.2, linetype = "solid") +
  geom_point(aes(y = same_high_conf), size = 3) +
  geom_line(aes(y = same_high_conf, group = condition), size = 1.2) +
  geom_linerange(aes(ymin = same_high_conf - same_high_conf_se, 
                     ymax = same_high_conf + same_high_conf_se, group = condition), 
                  linetype = "solid") +
  geom_point(aes(y = same_low_conf), size = 3) +
  geom_line(aes(y = same_low_conf, group = condition), size = 1.2, linetype = "solid") +
  geom_linerange(aes(ymin = same_low_conf - same_low_conf_se, 
                     ymax = same_low_conf + same_low_conf_se, group = condition), 
                 width = 0.2, linetype = "solid") +
  ggtitle("Average of Confidence Response - ADT") +
  labs(caption = "Error bars represent within-subject SEM", x = "Oddball Level", y = "Confidence Rating") +
  scale_color_manual(values = c("#BC3C29FF", "#ea9999", "#0072B5FF", "#CCE6FF"),
                     name = "Response Type") +
  ylim(2,4)+
  scale_linetype_manual(values = c("solid", "solid"), name = "Condition",
                        labels = c("High Grip", "Low Grip")) 

6.3 Statistical Analysis

6.3.1 Effect of Grip on responses

6.3.1.1 The Effect of Grip on ‘Different’ Responses across Stimulus Levels

ADT_anova_grip_diff <- ADT_4T %>%
  filter(stimLev!= 0) %>% group_by(stimLev, isStrength, sub) %>% mutate(diff_sub = mean(resp1_diff, na.rm = TRUE)) %>%
  ungroup() %>% group_by(stimLev, isStrength) %>% summarise(diff = mean(diff_sub), stimLev, isStrength) %>%
  distinct()
  
head(ADT_anova_grip_diff)
aov(diff ~ (stimLev * isStrength), ADT_anova_grip_diff) %>% report()
## The ANOVA (formula: diff ~ (stimLev * isStrength)) suggests that:
## 
##   - The main effect of stimLev is statistically not significant and large (F(1,
## 4) = 7.23, p = 0.055; Eta2 (partial) = 0.64, 95% CI [0.00, 1.00])
##   - The main effect of isStrength is statistically not significant and very small
## (F(1, 4) = 1.67e-03, p = 0.969; Eta2 (partial) = 4.17e-04, 95% CI [0.00, 1.00])
##   - The interaction between stimLev and isStrength is statistically not
## significant and very small (F(1, 4) = 7.88e-05, p = 0.993; Eta2 (partial) =
## 1.97e-05, 95% CI [0.00, 1.00])
## 
## Effect sizes were labelled following Field's (2013) recommendations.

6.3.1.2 The Effect of Grip on ‘Same’ Responses across Stimulus Levels

ADT_anova_grip_same <- ADT_4T %>%
  filter(stimLev!= 0) %>% group_by(stimLev, isStrength, sub) %>% mutate(diff_sub = 1-mean(resp1_diff, na.rm = TRUE)) %>%
  ungroup() %>% group_by(stimLev, isStrength) %>% summarise(diff = mean(diff_sub, na.rm = T), stimLev, isStrength) %>%
  distinct()
  
head(ADT_anova_grip_same)
aov(diff ~ (stimLev * isStrength), ADT_anova_grip_same) %>% report()
## The ANOVA (formula: diff ~ (stimLev * isStrength)) suggests that:
## 
##   - The main effect of stimLev is statistically not significant and large (F(1,
## 4) = 7.23, p = 0.055; Eta2 (partial) = 0.64, 95% CI [0.00, 1.00])
##   - The main effect of isStrength is statistically not significant and very small
## (F(1, 4) = 1.67e-03, p = 0.969; Eta2 (partial) = 4.17e-04, 95% CI [0.00, 1.00])
##   - The interaction between stimLev and isStrength is statistically not
## significant and very small (F(1, 4) = 7.88e-05, p = 0.993; Eta2 (partial) =
## 1.97e-05, 95% CI [0.00, 1.00])
## 
## Effect sizes were labelled following Field's (2013) recommendations.

6.3.2 Effect of Grip on Reaction Time

6.3.2.1 The Effect of Grip on Median Reaction Time for ‘Different’ responses

ADT_anova_grip_RT_diff <- ADT_4T %>%
  filter(stimLev!= 0) %>% filter(resp1_diff ==1) %>% group_by(stimLev, isStrength, sub) %>% mutate(RT_sub = median(resp1RT)) %>%
  ungroup() %>% group_by(stimLev, isStrength) %>% summarise(RT = median(RT_sub), stimLev, isStrength) %>%
  distinct()
  
head(ADT_anova_grip_RT_diff)
aov(RT ~ (stimLev * isStrength), ADT_anova_grip_RT_diff) %>% report()
## The ANOVA (formula: RT ~ (stimLev * isStrength)) suggests that:
## 
##   - The main effect of stimLev is statistically not significant and large (F(1,
## 4) = 5.45, p = 0.080; Eta2 (partial) = 0.58, 95% CI [0.00, 1.00])
##   - The main effect of isStrength is statistically not significant and small
## (F(1, 4) = 0.14, p = 0.731; Eta2 (partial) = 0.03, 95% CI [0.00, 1.00])
##   - The interaction between stimLev and isStrength is statistically not
## significant and small (F(1, 4) = 0.08, p = 0.797; Eta2 (partial) = 0.02, 95% CI
## [0.00, 1.00])
## 
## Effect sizes were labelled following Field's (2013) recommendations.

6.3.2.2 The Effect of Grip on Median Reaction Time for ‘Same’ responses

ADT_anova_grip_RT_same <- ADT_4T %>%
  filter(stimLev!= 0) %>% filter(resp1_diff ==0) %>% group_by(stimLev, isStrength, sub) %>% mutate(RT_sub = median(resp1RT)) %>%
  ungroup() %>% group_by(stimLev, isStrength) %>% summarise(RT = median(RT_sub), stimLev, isStrength) %>%
  distinct()
  
head(ADT_anova_grip_RT_same)
aov(RT ~ (stimLev * isStrength), ADT_anova_grip_RT_same) %>% report()
## The ANOVA (formula: RT ~ (stimLev * isStrength)) suggests that:
## 
##   - The main effect of stimLev is statistically not significant and large (F(1,
## 4) = 6.36, p = 0.065; Eta2 (partial) = 0.61, 95% CI [0.00, 1.00])
##   - The main effect of isStrength is statistically not significant and large
## (F(1, 4) = 0.73, p = 0.440; Eta2 (partial) = 0.15, 95% CI [0.00, 1.00])
##   - The interaction between stimLev and isStrength is statistically not
## significant and large (F(1, 4) = 3.10, p = 0.153; Eta2 (partial) = 0.44, 95% CI
## [0.00, 1.00])
## 
## Effect sizes were labelled following Field's (2013) recommendations.

6.3.3 Effect of Grip on Confidence

6.3.3.1 The Effect of Grip on Confidence Reports for ‘Different’ responses

ADT_anova_grip_conf_diff <- ADT_4T %>%
  filter(stimLev!= 0) %>% filter(resp1_diff ==1) %>% group_by(stimLev, isStrength, sub) %>% mutate(conf_sub = mean(resp2, na.rm = T)) %>%
  ungroup() %>% group_by(stimLev, isStrength) %>% summarise(conf = mean(conf_sub, na.rm = T), stimLev, isStrength) %>%
  distinct()
  
head(ADT_anova_grip_conf_diff)
aov(conf ~ (stimLev * isStrength), ADT_anova_grip_conf_diff) %>% report()
## The ANOVA (formula: conf ~ (stimLev * isStrength)) suggests that:
## 
##   - The main effect of stimLev is statistically significant and large (F(1, 4) =
## 7.81, p = 0.049; Eta2 (partial) = 0.66, 95% CI [3.12e-03, 1.00])
##   - The main effect of isStrength is statistically not significant and very small
## (F(1, 4) = 0.02, p = 0.894; Eta2 (partial) = 5.01e-03, 95% CI [0.00, 1.00])
##   - The interaction between stimLev and isStrength is statistically not
## significant and very small (F(1, 4) = 2.88e-03, p = 0.960; Eta2 (partial) =
## 7.19e-04, 95% CI [0.00, 1.00])
## 
## Effect sizes were labelled following Field's (2013) recommendations.

6.3.3.2 The Effect of Grip on Confidence Reports for ‘Same’ responses

ADT_anova_grip_conf_same <- ADT_4T %>%
  filter(stimLev!= 0) %>% filter(resp1_diff ==0) %>% group_by(stimLev, isStrength, sub) %>% mutate(conf_sub = mean(resp2, na.rm = T)) %>%
  ungroup() %>% group_by(stimLev, isStrength) %>% summarise(conf = median(conf_sub, na.rm=T), stimLev, isStrength) %>%
  distinct()
  
head(ADT_anova_grip_conf_same)
aov(conf ~ (stimLev * isStrength), ADT_anova_grip_conf_same) %>% report()
## The ANOVA (formula: conf ~ (stimLev * isStrength)) suggests that:
## 
##   - The main effect of stimLev is statistically significant and large (F(1, 4) =
## 23.99, p = 0.008; Eta2 (partial) = 0.86, 95% CI [0.38, 1.00])
##   - The main effect of isStrength is statistically not significant and very small
## (F(1, 4) = 0.02, p = 0.895; Eta2 (partial) = 4.91e-03, 95% CI [0.00, 1.00])
##   - The interaction between stimLev and isStrength is statistically not
## significant and very small (F(1, 4) = 0.01, p = 0.915; Eta2 (partial) =
## 3.19e-03, 95% CI [0.00, 1.00])
## 
## Effect sizes were labelled following Field's (2013) recommendations.

6.4 VDT

6.4.1 Specific dataframe

VDT_4T <- lcya_behdata_trial_avdt %>%
  filter(task=='vis') %>%
  filter(!is.na(resp1)) %>%
  filter(sub %in% c('13','16','17','18','19','23','24','25','26','27','29','31','33','34','35','36','37','42','43','46','48','54','56','58','61','64','65','68','69','71','72','74','82','83','85','87','88'))


## Subject 25 entered same and different in a reverse manner
VDT_4T <- VDT_4T %>%
   mutate(
    resp1 = ifelse(sub == "25" & resp1 == 1, 2, 
                   ifelse(sub == "25" & resp1 == 2, 1, resp1)),  # Switch 1 and 2 in resp1 for 25
    iscorr = ifelse(sub == "25" & isOddball == resp1, 1, 
                    ifelse(sub == "25", 0, iscorr)),  # Update isCorr based on isOddball and resp1 for 25
    hit = ifelse(sub == "25" & isOddball == 1 & resp1 == 2, 1, 
                  ifelse(sub == "25", 0, hit)),  # Update hit based on isOddball and resp1 for 25
    miss = ifelse(sub == "25" & isOddball == 1 & resp1 == 1, 1, 
                   ifelse(sub == "25", 0, miss)),  # Update miss based on isOddball and resp1 for 25
    fa = ifelse(sub == "25" & isOddball == 0 & resp1 == 2, 1, 
                 ifelse(sub == "25", 0, fa)),  # Update fa based on isOddball and resp1 for 25
    cr = ifelse(sub == "25" & isOddball == 0 & resp1 == 1, 1, 
                 ifelse(sub == "25", 0, cr)),  # Update cr based on isOddball and resp1 for 25
    resp1_diff = ifelse(sub == "25", 1 - resp1_diff, resp1_diff)  # Switch 0s and 1s in resp1_diff for 25
  )

6.4.2 Proportion of Responding ‘Different’

# calculate subject mean
VDT_4T_acc_ws <- VDT_4T %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1_diff, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
VDT_4T_acc_ws <- VDT_4T_acc_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

VDT_4T_acc_ws <- VDT_4T_acc_ws %>%
  mutate(acc_diff = sub_mean- grand_mean) 

# Calculate mean accuracy and standard error for 'Different'
Diff_prop_VDT_4T <- VDT_4T_acc_ws %>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(diff=mean(diff_sub), diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate the proportion of 'Same'
Same_prop_VDT_4T <- VDT_4T_acc_ws %>%
    group_by(sub, stimLev) %>%
    summarise(same_sub = 1-mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(not_diff=mean(same_sub), not_diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

# Combine both datasets for plotting
combined_VDT_4T <- bind_rows(
  mutate(Diff_prop_VDT_4T, condition = "Different", prop = diff),
  mutate(Same_prop_VDT_4T, condition = "Same", prop = not_diff)
)

# Specify the desired order of levels for 'stimLev'
desired_order <- c("0", "0.04", "0.08", "0.16", "0.32")

# Convert 'stimLev' to factor with the desired order
combined_VDT_4T$stimLev <- factor(combined_VDT_4T$stimLev, levels = desired_order)

# Plotting with the modified 'stimLev'
ggplot(combined_VDT_4T, aes(x = stimLev, y = prop, group = condition, color = condition)) +
  geom_point(size = 2) +
  geom_line(size = 1.2) +
  geom_linerange(mapping = aes(ymin = prop - ifelse(condition == "Different", diff_se, not_diff_se), 
                                ymax = prop + ifelse(condition == "Different", diff_se, not_diff_se))) +
  ylim(0, 1) +
  ggtitle("Proportion of Responding 'Different' and 'Same' - VDT") +
  xlab("Contrast Level") +
  ylab("Proportion") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("#BC3C29FF", "#0072B5FF")) +
  guides(color = guide_legend(title = "condition"))

6.4.2.1 Handgrip manipulation

# calculate subject mean
VDT_4T_acc_ws <- VDT_4T %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1_diff, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
VDT_4T_acc_ws <- VDT_4T_acc_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

VDT_4T_acc_ws <- VDT_4T_acc_ws %>%
  mutate(acc_diff = sub_mean- grand_mean) 

# Calculate mean accuracy and standard error for 'Different'
Diff_prop_high_grip_VDT_4T <- VDT_4T_acc_ws %>%
    filter(isStrength==1)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(diff=mean(diff_sub), diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

Diff_prop_low_grip_VDT_4T <- VDT_4T_acc_ws %>%
    filter(isStrength==0)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(diff=mean(diff_sub), diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate the proportion of 'Same'
Same_prop_high_grip_VDT_4T <- VDT_4T_acc_ws %>%
    filter(isStrength==1) %>%
    group_by(sub, stimLev) %>%
    summarise(same_sub = 1-mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(not_diff=mean(same_sub), not_diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

Same_prop_low_grip_VDT_4T <- VDT_4T_acc_ws %>%
    filter(isStrength==0) %>%
    group_by(sub, stimLev) %>%
    summarise(same_sub = 1-mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(not_diff=mean(same_sub), not_diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

# Combine both datasets for plotting
combined_grip_VDT_4T <- bind_rows(
  mutate(Diff_prop_high_grip_VDT_4T, condition = "Different - High Grip", prop = diff),
  mutate(Diff_prop_low_grip_VDT_4T, condition = "Different - Low Grip", prop = diff),
  mutate(Same_prop_high_grip_VDT_4T, condition = "Same - High Grip", prop = not_diff),
  mutate(Same_prop_low_grip_VDT_4T, condition = "Same - Low Grip", prop = not_diff)
)

# Specify the desired order of levels for 'stimLev'
desired_order <- c("0", "0.04", "0.08", "0.16", "0.32")

# Convert 'stimLev' to factor with the desired order
combined_grip_VDT_4T$stimLev <- factor(combined_grip_VDT_4T$stimLev, levels = desired_order)

# Plotting
ggplot(combined_grip_VDT_4T, aes(x = stimLev, y = prop, group = condition, color = condition)) +
  geom_point( size = 2) +
  geom_line(size = 1.2) +
  geom_linerange(mapping = aes(ymin = prop - ifelse(condition == "Different - High Grip" | condition == "Different - Low Grip", diff_se, not_diff_se), 
                               ymax = prop + ifelse(condition == "Different - High Grip" | condition == "Different - Low Grip", diff_se, not_diff_se)), show.legend = FALSE) +
  ylim(0, 1) +
  ggtitle("Proportion of Responding 'Different' and 'Same' - VDT") +
  xlab("Contrast Level") +
  ylab("Proportion") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("Different - High Grip" = "#BC3C29FF", "Different - Low Grip" = "#ea9999", "Same - High Grip" = "#0072B5FF", "Same - Low Grip" = "#CCE6FF")) +
  scale_linetype_manual(values = c("Different - High Grip" = "solid", "Different - Low Grip" = "solid", "Same - High Grip" = "solid", "Same - Low Grip" = "solid")) +
  guides(color = guide_legend(title = "Response Type", ncol = 1), linetype = "none")

6.4.3 Reaction Time

6.4.3.1 Using Median with within-subject SEM

# calculate subject mean
VDT_4T_RT_ws <- VDT_4T %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1RT, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
VDT_4T_RT_ws <- VDT_4T_RT_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

VDT_4T_RT_ws <- VDT_4T_RT_ws %>%
  mutate(RT_diff = sub_mean- grand_mean) 


# Calculate median RT for different responses
VDT_4T_diff_RT_ws <- VDT_4T_RT_ws %>%
  filter(resp1_diff == 1) %>%
  group_by(stimLev,sub) %>%
  summarise(diff_sub= median(resp1RT), RT_diff) %>%
  ungroup() %>%
  group_by(stimLev) %>%
  summarise(medRT_diff = median(diff_sub),
            medRT_diff_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
  distinct()

# Calculate median RT for same responses
VDT_4T_same_RT_ws <- VDT_4T_RT_ws %>%
  filter(resp1_diff == 0) %>%
  group_by(stimLev,sub) %>%
  summarise(diff_sub= median(resp1RT), RT_diff) %>%
  ungroup() %>%
  group_by(stimLev) %>%
  summarise(medRT_same = median(diff_sub),
            medRT_same_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
  distinct()

# Combine both datasets for plotting
combined_medRT_VDT_4T <- bind_rows(
  mutate(VDT_4T_diff_RT_ws, condition = "Different"),
  mutate(VDT_4T_same_RT_ws, condition = "Same")
)


# Specify the desired order of levels for 'stimLev'
    desired_order <- c("0", "0.04", "0.08", "0.16", "0.32")

# Convert 'stimLev' to factor with the desired order
combined_medRT_VDT_4T$stimLev <- factor(combined_medRT_VDT_4T$stimLev, levels = desired_order)

#Sort the combined dataset by stimLev within each condition
combined_medRT_VDT_4T <- combined_medRT_VDT_4T %>%
  arrange(condition, stimLev)

# Plotting
ggplot(combined_medRT_VDT_4T, aes(x = stimLev, group = condition, color = condition, linetype= condition)) +
  geom_point(aes(y = ifelse(condition == "Different", medRT_diff , medRT_same)), size = 2) +
  geom_line(aes(y = ifelse(condition == "Different", medRT_diff, medRT_same)), size = 1.2) +
  ylim(min(c(combined_medRT_VDT_4T$medRT_diff, combined_medRT_VDT_4T$medRT_same)), max(c(combined_medRT_VDT_4T$medRT_diff, combined_medRT_VDT_4T$medRT_same))) +
  ggtitle("Median of Reaction Time for Same and Different Responses - VDT") +
  geom_linerange(mapping = aes(ymin = medRT_diff - medRT_diff_se, ymax = medRT_diff + medRT_diff_se, linetype = condition), data = filter(combined_medRT_VDT_4T, condition == "Different")) +
  geom_linerange(mapping = aes(ymin = medRT_same - medRT_same_se, ymax = medRT_same + medRT_same_se, linetype = condition), data = filter(combined_medRT_VDT_4T, condition == "Same")) +
  labs(caption = "Error bars represent within-subject SEM") +
  xlab("Contrast Level") +
  ylab("Reaction Time (S)") +
  scale_color_manual(values = c( "#BC3C29FF", "#0072B5FF")) +
  scale_linetype_manual(values = c("Different" = "solid", "Same" = "solid")) 

6.4.3.2 Using Median within and Mean between subjects with WSSEM

# calculate subject mean
VDT_4T_RT_ws <- VDT_4T %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1RT, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
VDT_4T_RT_ws <- VDT_4T_RT_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

VDT_4T_RT_ws <- VDT_4T_RT_ws %>%
  mutate(RT_diff = sub_mean- grand_mean) 


# Calculate median RT for different responses
VDT_4T_diff_RT_ws <- VDT_4T_RT_ws %>%
  filter(resp1_diff == 1) %>%
  group_by(stimLev,sub) %>%
  summarise(diff_sub= median(resp1RT), RT_diff) %>%
  ungroup() %>%
  group_by(stimLev) %>%
  summarise(medRT_diff = mean(diff_sub, na.rm = T),
            medRT_diff_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
  distinct()

# Calculate median RT for same responses
VDT_4T_same_RT_ws <- VDT_4T_RT_ws %>%
  filter(resp1_diff == 0) %>%
  group_by(stimLev,sub) %>%
  summarise(diff_sub= median(resp1RT), RT_diff) %>%
  ungroup() %>%
  group_by(stimLev) %>%
  summarise(medRT_same = mean(diff_sub, na.rm = T),
            medRT_same_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
  distinct()

# Combine both datasets for plotting
combined_RT_VDT_4T <- bind_rows(
  mutate(VDT_4T_diff_RT_ws, condition = "Different"),
  mutate(VDT_4T_same_RT_ws, condition = "Same")
)


# Specify the desired order of levels for 'stimLev'
    desired_order <- c("0", "0.04", "0.08", "0.16", "0.32")

# Convert 'stimLev' to factor with the desired order
combined_RT_VDT_4T$stimLev <- factor(combined_RT_VDT_4T$stimLev, levels = desired_order)

#Sort the combined dataset by stimLev within each condition
combined_RT_VDT_4T <- combined_RT_VDT_4T %>%
  arrange(condition, stimLev)

# Plotting
ggplot(combined_RT_VDT_4T, aes(x = stimLev, group = condition, color = condition, linetype= condition)) +
  geom_point(aes(y = ifelse(condition == "Different", medRT_diff , medRT_same)), size = 2) +
  geom_line(aes(y = ifelse(condition == "Different", medRT_diff, medRT_same)), size = 1.2) +
  ylim(min(c(combined_RT_VDT_4T$medRT_diff, combined_RT_VDT_4T$medRT_same)), max(c(combined_RT_VDT_4T$medRT_diff, combined_RT_VDT_4T$medRT_same))) +
  ggtitle("Median of Within and Mean Between Reaction Time  - VDT") +
  geom_linerange(mapping = aes(ymin = medRT_diff - medRT_diff_se, ymax = medRT_diff + medRT_diff_se, linetype = condition), data = filter(combined_RT_VDT_4T, condition == "Different")) +
  geom_linerange(mapping = aes(ymin = medRT_same - medRT_same_se, ymax = medRT_same + medRT_same_se, linetype = condition), data = filter(combined_RT_VDT_4T, condition == "Same")) +
  labs(caption = "Error bars represent within-subject SEM") +
  xlab("Contrast Level") +
  ylab("Reaction Time (S)") +
  scale_color_manual(values = c( "#BC3C29FF", "#0072B5FF")) +
  scale_linetype_manual(values = c("Different" = "solid", "Same" = "solid")) 

6.4.3.3 Handgrip manipulation with Median

# calculate subject mean
VDT_4T_RT_ws <- VDT_4T %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1RT, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calcualte the grand mean
VDT_4T_RT_ws <- VDT_4T_RT_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

VDT_4T_RT_ws <- VDT_4T_RT_ws %>%
  mutate(RT_diff = sub_mean- grand_mean) 

# Calculate mean RT and standard error for  High 'Different'
RT_diff_high_grip_VDT_4T <- VDT_4T_RT_ws %>%
    filter(resp1_diff == 1) %>%
    filter(isStrength==1)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_high_diff=median(diff_sub), medRT_high_diff_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate mean RT and standard error for  Low 'Different'
RT_diff_low_grip_VDT_4T <- VDT_4T_RT_ws %>%
    filter(resp1_diff == 1) %>%
    filter(isStrength==0)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_low_diff=median(diff_sub), medRT_low_diff_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()


# Calculate mean RT and standard error for  High 'Same'
RT_same_high_grip_VDT_4T <- VDT_4T_RT_ws %>%
    filter(resp1_diff == 0) %>%
    filter(isStrength==1)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_high_same=median(diff_sub), medRT_high_same_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate mean RT and standard error for  Low 'Same'
RT_same_low_grip_VDT_4T <- VDT_4T_RT_ws %>%
    filter(resp1_diff == 0) %>%
    filter(isStrength==0)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_low_same=median(diff_sub), medRT_low_same_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()


# Combine both datasets for plotting
combined_medRT_grip_VDT_4T <- bind_rows(
  mutate(RT_diff_high_grip_VDT_4T, condition = "Different - High Grip"),
  mutate(RT_diff_low_grip_VDT_4T, condition = "Different - Low Grip"),
  mutate(RT_same_high_grip_VDT_4T, condition = "Same - High Grip"),
  mutate(RT_same_low_grip_VDT_4T, condition = "Same - Low Grip")
)

# Specify the desired order of levels for 'stimLev'
    desired_order <- c("0", "0.04", "0.08", "0.16", "0.32")

# Convert 'stimLev' to factor with the desired order
combined_medRT_grip_VDT_4T$stimLev <- factor(combined_medRT_grip_VDT_4T$stimLev, levels = desired_order)


# Plotting without jitter
ggplot(data = combined_medRT_grip_VDT_4T) +
  geom_point(aes(x = stimLev, y = medRT_high_diff, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_high_diff, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_high_diff - medRT_high_diff_se, 
                     ymax = medRT_high_diff + medRT_high_diff_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_low_diff, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_low_diff, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_low_diff - medRT_low_diff_se, 
                     ymax = medRT_low_diff + medRT_low_diff_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_high_same, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_high_same, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_high_same - medRT_high_same_se, 
                     ymax = medRT_high_same + medRT_high_same_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_low_same, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_low_same, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_low_same - medRT_low_same_se, 
                     ymax = medRT_low_same + medRT_low_same_se, color = condition), 
                 linetype = "solid") +
  
  ggtitle("Median of Reaction Time for Same and Different Responses - VDT") +
  xlab("Contrast Level") +
  ylab("Reaction Time") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("#BC3C29FF", "#ea9999", "#0072B5FF", "#CCE6FF"),
                      name = "Condition") 

6.4.3.4 Handgrip manipulation with Median within and Mean between with WSSEM

# calculate subject mean
VDT_4T_RT_ws <- VDT_4T %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1RT, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calcualte the grand mean
VDT_4T_RT_ws <- VDT_4T_RT_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

VDT_4T_RT_ws <- VDT_4T_RT_ws %>%
  mutate(RT_diff = sub_mean- grand_mean) 

# Calculate mean RT and standard error for  High 'Different'
RT_diff_high_grip_VDT_4T <- VDT_4T_RT_ws %>%
    filter(resp1_diff == 1) %>%
    filter(isStrength==1)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_high_diff=mean(diff_sub, na.rm = T), medRT_high_diff_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate mean RT and standard error for  Low 'Different'
RT_diff_low_grip_VDT_4T <- VDT_4T_RT_ws %>%
    filter(resp1_diff == 1) %>%
    filter(isStrength==0)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_low_diff=mean(diff_sub, na.rm = T), medRT_low_diff_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()


# Calculate mean RT and standard error for  High 'Same'
RT_same_high_grip_VDT_4T <- VDT_4T_RT_ws %>%
    filter(resp1_diff == 0) %>%
    filter(isStrength==1)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_high_same=mean(diff_sub, na.rm = T), medRT_high_same_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate mean RT and standard error for  Low 'Same'
RT_same_low_grip_VDT_4T <- VDT_4T_RT_ws %>%
    filter(resp1_diff == 0) %>%
    filter(isStrength==0)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_low_same=mean(diff_sub, na.rm = T), medRT_low_same_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()


# Combine both datasets for plotting
combined_RT_grip_VDT_4T <- bind_rows(
  mutate(RT_diff_high_grip_VDT_4T, condition = "Different - High Grip"),
  mutate(RT_diff_low_grip_VDT_4T, condition = "Different - Low Grip"),
  mutate(RT_same_high_grip_VDT_4T, condition = "Same - High Grip"),
  mutate(RT_same_low_grip_VDT_4T, condition = "Same - Low Grip")
)

# Specify the desired order of levels for 'stimLev'
    desired_order <- c("0", "0.04", "0.08", "0.16", "0.32")

# Convert 'stimLev' to factor with the desired order
combined_RT_grip_VDT_4T$stimLev <- factor(combined_RT_grip_VDT_4T$stimLev, levels = desired_order)


# Plotting without jitter
ggplot(data = combined_RT_grip_VDT_4T) +
  geom_point(aes(x = stimLev, y = medRT_high_diff, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_high_diff, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_high_diff - medRT_high_diff_se, 
                     ymax = medRT_high_diff + medRT_high_diff_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_low_diff, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_low_diff, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_low_diff - medRT_low_diff_se, 
                     ymax = medRT_low_diff + medRT_low_diff_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_high_same, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_high_same, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_high_same - medRT_high_same_se, 
                     ymax = medRT_high_same + medRT_high_same_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_low_same, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_low_same, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_low_same - medRT_low_same_se, 
                     ymax = medRT_low_same + medRT_low_same_se, color = condition), 
                 linetype = "solid") +
  
  ggtitle("Median of Reaction Time Within and Mean Between - VDT") +
  xlab("Contrast Level") +
  ylab("Reaction Time") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("#BC3C29FF", "#ea9999", "#0072B5FF", "#CCE6FF"),
                      name = "Condition") 

6.4.4 Confidence

# calculate subject mean
VDT_4T_conf_ws <- VDT_4T %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp2, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
VDT_4T_conf_ws <- VDT_4T_conf_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

VDT_4T_conf_ws <- VDT_4T_conf_ws %>%
  mutate(conf_diff = sub_mean- grand_mean) 

diff_average_confidence_VDT_4T <- VDT_4T_conf_ws %>%
        filter(resp1_diff== 1) %>%
        group_by(stimLev, sub) %>%
        summarise(diff_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(diff_conf = mean(diff_prop, na.rm = T), 
                  diff_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()

same_average_confidence_VDT_4T <- VDT_4T_conf_ws %>%
        filter(resp1_diff== 0) %>%
        group_by(stimLev, sub) %>%
        summarise(same_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(same_conf = mean(same_prop), 
                  same_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()

  combined_prop_VDT_4T <- bind_rows(
        mutate(diff_average_confidence_VDT_4T, condition = "Different"),
        mutate(same_average_confidence_VDT_4T, condition = "Same")
    )
  
    # Specify the desired order of levels for 'stimLev'
    desired_order <- c("0", "0.04", "0.08", "0.16", "0.32")
    
    # Convert 'stimLev' to factor with the desired order
    combined_prop_VDT_4T$stimLev <- factor(combined_prop_VDT_4T$stimLev, levels = desired_order)
    
    # Sort the combined dataset by stimLev within each condition
    combined_prop_VDT_4T <- combined_prop_VDT_4T %>%
        arrange(condition, stimLev)
    
    # Plotting
    ggplot(combined_prop_VDT_4T, aes(x = stimLev, group = condition, color = condition)) +
        geom_point(aes(y = ifelse(condition == "Different", diff_conf, same_conf)), size = 2) +
    geom_line(aes(y = ifelse(condition == "Different", diff_conf, same_conf)), size = 1.2) +
        ylim(2, 4) +
        ggtitle("Average of Confidence Response - VDT") +
        geom_linerange(mapping = aes(ymin = diff_conf - diff_conf_se, ymax = diff_conf + diff_conf_se, linetype = condition), data = filter(combined_prop_VDT_4T, condition == "Different")) +
    geom_linerange(mapping = aes(ymin = same_conf - same_conf_se, ymax = same_conf + same_conf_se, linetype = condition), data = filter(combined_prop_VDT_4T, condition == "Same")) +
        labs(caption = "Error bars represent within-subject SEM") +
        xlab("Contrast Level") +
        ylab("Confidence Rating") +
        scale_color_manual(values = c("Different" = "#BC3C29FF",
                                      "Same" = "#0072B5FF"
                                      ),
                           labels = c("Different",
                                      "Same"
                                      )) +
        scale_linetype_manual(values = c("Different" = "solid", "Same" = "solid"))

6.4.4.1 Handgrip manipulation

# calculate subject mean
VDT_4T_conf_ws <- VDT_4T %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp2, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
VDT_4T_conf_ws <- VDT_4T_conf_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

VDT_4T_conf_ws <- VDT_4T_conf_ws %>%
  mutate(conf_diff = sub_mean- grand_mean) 

diff_average_high_confidence_VDT_4T <- VDT_4T_conf_ws %>%
        filter(isStrength == 1) %>%
        filter(resp1_diff== 1) %>%
        group_by(stimLev, sub) %>%
        summarise(diff_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(diff_high_conf = mean(diff_prop, na.rm = T), 
                  diff_high_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()

diff_average_low_confidence_VDT_4T <- VDT_4T_conf_ws %>%
        filter(isStrength == 0) %>%
        filter(resp1_diff== 1) %>%
        group_by(stimLev, sub) %>%
        summarise(diff_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(diff_low_conf = mean(diff_prop, na.rm = T), 
                  diff_low_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()


same_average_high_confidence_VDT_4T <- VDT_4T_conf_ws %>%
        filter(isStrength == 1) %>%
        filter(resp1_diff== 0) %>%
        group_by(stimLev, sub) %>%
        summarise(same_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(same_high_conf = mean(same_prop, na.rm = T), 
                  same_high_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()

same_average_low_confidence_VDT_4T <- VDT_4T_conf_ws %>%
        filter(isStrength == 0) %>%
        filter(resp1_diff== 0) %>%
        group_by(stimLev, sub) %>%
        summarise(same_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(same_low_conf = mean(same_prop, na.rm = T), 
                  same_low_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()


combined_conf_grip_VDT_4T <- bind_rows(
        mutate(diff_average_high_confidence_VDT_4T, condition = "Different - High Grip"),
        mutate(diff_average_low_confidence_VDT_4T, condition = "Different - Low Grip" ),
        mutate(same_average_high_confidence_VDT_4T, condition = "Same - High Grip"),
        mutate(same_average_low_confidence_VDT_4T, condition = "Same - Low Grip")
)
  
    # Specify the desired order of levels for 'stimLev'
    desired_order <- c("0", "0.04", "0.08", "0.16", "0.32")
    
    # Convert 'stimLev' to factor with the desired order
    combined_conf_grip_VDT_4T$stimLev <- factor(combined_conf_grip_VDT_4T$stimLev, levels = desired_order)
    
    # Sort the combined dataset by stimLev within each condition
    combined_conf_grip_VDT_4T <- combined_conf_grip_VDT_4T %>%
        arrange(condition, stimLev)
    


ggplot(combined_conf_grip_VDT_4T, aes(x = stimLev, color = condition)) +
  geom_point(aes(y = diff_high_conf), size = 3) +
  geom_line(aes(y = diff_high_conf, group = condition), size = 1.2) +
  geom_linerange(aes(ymin = diff_high_conf - diff_high_conf_se, 
                     ymax = diff_high_conf + diff_high_conf_se, group = condition), 
                 width = 0.2, linetype = "solid") +
  geom_point(aes(y = diff_low_conf), size = 3) +
  geom_line(aes(y = diff_low_conf, group = condition), size = 1.2, linetype = "solid") +
  geom_linerange(aes(ymin = diff_low_conf - diff_low_conf_se, 
                     ymax = diff_low_conf + diff_low_conf_se, group = condition), 
                 width = 0.2, linetype = "solid") +
  geom_point(aes(y = same_high_conf), size = 3) +
  geom_line(aes(y = same_high_conf, group = condition), size = 1.2) +
  geom_linerange(aes(ymin = same_high_conf - same_high_conf_se, 
                     ymax = same_high_conf + same_high_conf_se, group = condition), 
                  linetype = "solid") +
  geom_point(aes(y = same_low_conf), size = 3) +
  geom_line(aes(y = same_low_conf, group = condition), size = 1.2, linetype = "solid") +
  geom_linerange(aes(ymin = same_low_conf - same_low_conf_se, 
                     ymax = same_low_conf + same_low_conf_se, group = condition), 
                 width = 0.2, linetype = "solid") +
  ggtitle("Average of Confidence Response - VDT") +
  ylim(2,4)+
  labs(caption = "Error bars represent within-subject SEM", x = "Oddball Level", y = "Confidence Rating") +
  scale_color_manual(values = c("#BC3C29FF", "#ea9999", "#0072B5FF", "#CCE6FF"),
                     name = "Response Type") +
  scale_linetype_manual(values = c("solid", "solid"), name = "Condition",
                        labels = c("High Grip", "Low Grip")) 

6.5 Statistical Analysis

6.5.1 Effect of Grip on responses

6.5.1.1 The Effect of Grip on ‘Different’ Responses across Stimulus Levels

VDT_anova_grip_diff <- VDT_4T %>%
  filter(stimLev!= 0) %>% group_by(stimLev, isStrength, sub) %>% mutate(diff_sub = mean(resp1_diff, na.rm = TRUE)) %>%
  ungroup() %>% group_by(stimLev, isStrength) %>% summarise(diff = mean(diff_sub), stimLev, isStrength) %>%
  distinct()
  
head(VDT_anova_grip_diff)
aov(diff ~ (stimLev * isStrength), VDT_anova_grip_diff) %>% report()
## The ANOVA (formula: diff ~ (stimLev * isStrength)) suggests that:
## 
##   - The main effect of stimLev is statistically significant and large (F(1, 4) =
## 20.91, p = 0.010; Eta2 (partial) = 0.84, 95% CI [0.32, 1.00])
##   - The main effect of isStrength is statistically not significant and very small
## (F(1, 4) = 0.01, p = 0.923; Eta2 (partial) = 2.61e-03, 95% CI [0.00, 1.00])
##   - The interaction between stimLev and isStrength is statistically not
## significant and very small (F(1, 4) = 6.67e-03, p = 0.939; Eta2 (partial) =
## 1.66e-03, 95% CI [0.00, 1.00])
## 
## Effect sizes were labelled following Field's (2013) recommendations.

6.5.1.2 The Effect of Grip on ‘Same’ Responses across Stimulus Levels

VDT_anova_grip_same <- VDT_4T %>%
  filter(stimLev!= 0) %>% group_by(stimLev, isStrength, sub) %>% mutate(diff_sub = 1-mean(resp1_diff, na.rm = TRUE)) %>%
  ungroup() %>% group_by(stimLev, isStrength) %>% summarise(diff = mean(diff_sub, na.rm = T), stimLev, isStrength) %>%
  distinct()
  
head(VDT_anova_grip_same)
aov(diff ~ (stimLev * isStrength), VDT_anova_grip_same) %>% report()
## The ANOVA (formula: diff ~ (stimLev * isStrength)) suggests that:
## 
##   - The main effect of stimLev is statistically significant and large (F(1, 4) =
## 20.91, p = 0.010; Eta2 (partial) = 0.84, 95% CI [0.32, 1.00])
##   - The main effect of isStrength is statistically not significant and very small
## (F(1, 4) = 0.01, p = 0.923; Eta2 (partial) = 2.61e-03, 95% CI [0.00, 1.00])
##   - The interaction between stimLev and isStrength is statistically not
## significant and very small (F(1, 4) = 6.67e-03, p = 0.939; Eta2 (partial) =
## 1.66e-03, 95% CI [0.00, 1.00])
## 
## Effect sizes were labelled following Field's (2013) recommendations.

6.5.2 Effect of Grip on Reaction Time

6.5.2.1 The Effect of Grip on Median Reaction Time for ‘Different’ responses

VDT_anova_grip_RT_diff <- VDT_4T %>%
  filter(stimLev!= 0) %>% filter(resp1_diff ==1) %>% group_by(stimLev, isStrength, sub) %>% mutate(RT_sub = median(resp1RT)) %>%
  ungroup() %>% group_by(stimLev, isStrength) %>% summarise(RT = median(RT_sub), stimLev, isStrength) %>%
  distinct()
  
head(VDT_anova_grip_RT_diff)
aov(RT ~ (stimLev * isStrength), VDT_anova_grip_RT_diff) %>% report()
## The ANOVA (formula: RT ~ (stimLev * isStrength)) suggests that:
## 
##   - The main effect of stimLev is statistically not significant and large (F(1,
## 4) = 5.97, p = 0.071; Eta2 (partial) = 0.60, 95% CI [0.00, 1.00])
##   - The main effect of isStrength is statistically not significant and medium
## (F(1, 4) = 0.56, p = 0.497; Eta2 (partial) = 0.12, 95% CI [0.00, 1.00])
##   - The interaction between stimLev and isStrength is statistically not
## significant and small (F(1, 4) = 0.07, p = 0.802; Eta2 (partial) = 0.02, 95% CI
## [0.00, 1.00])
## 
## Effect sizes were labelled following Field's (2013) recommendations.

6.5.2.2 The Effect of Grip on Median Reaction Time for ‘Same’ responses

VDT_anova_grip_RT_same <- VDT_4T %>%
  filter(stimLev!= 0) %>% filter(resp1_diff ==0) %>% group_by(stimLev, isStrength, sub) %>% mutate(RT_sub = median(resp1RT)) %>%
  ungroup() %>% group_by(stimLev, isStrength) %>% summarise(RT = median(RT_sub), stimLev, isStrength) %>%
  distinct()
  
head(VDT_anova_grip_RT_same)
aov(RT ~ (stimLev * isStrength), VDT_anova_grip_RT_same) %>% report()
## The ANOVA (formula: RT ~ (stimLev * isStrength)) suggests that:
## 
##   - The main effect of stimLev is statistically significant and large (F(1, 4) =
## 67.91, p = 0.001; Eta2 (partial) = 0.94, 95% CI [0.71, 1.00])
##   - The main effect of isStrength is statistically not significant and large
## (F(1, 4) = 2.64, p = 0.180; Eta2 (partial) = 0.40, 95% CI [0.00, 1.00])
##   - The interaction between stimLev and isStrength is statistically not
## significant and large (F(1, 4) = 4.13, p = 0.112; Eta2 (partial) = 0.51, 95% CI
## [0.00, 1.00])
## 
## Effect sizes were labelled following Field's (2013) recommendations.

6.5.3 Effect of Grip on Confidence

6.5.3.1 The Effect of Grip on Confidence Reports for ‘Different’ responses

VDT_anova_grip_conf_diff <- VDT_4T %>%
  filter(stimLev!= 0) %>% filter(resp1_diff ==1) %>% group_by(stimLev, isStrength, sub) %>% mutate(conf_sub = mean(resp2, na.rm = T)) %>%
  ungroup() %>% group_by(stimLev, isStrength) %>% summarise(conf = mean(conf_sub, na.rm = T), stimLev, isStrength) %>%
  distinct()
  
head(VDT_anova_grip_conf_diff)
aov(conf ~ (stimLev * isStrength), VDT_anova_grip_conf_diff) %>% report()
## The ANOVA (formula: conf ~ (stimLev * isStrength)) suggests that:
## 
##   - The main effect of stimLev is statistically significant and large (F(1, 4) =
## 48.90, p = 0.002; Eta2 (partial) = 0.92, 95% CI [0.62, 1.00])
##   - The main effect of isStrength is statistically not significant and very small
## (F(1, 4) = 6.26e-04, p = 0.981; Eta2 (partial) = 1.57e-04, 95% CI [0.00, 1.00])
##   - The interaction between stimLev and isStrength is statistically not
## significant and very small (F(1, 4) = 6.52e-04, p = 0.981; Eta2 (partial) =
## 1.63e-04, 95% CI [0.00, 1.00])
## 
## Effect sizes were labelled following Field's (2013) recommendations.

6.5.3.2 The Effect of Grip on Confidence Reports for ‘Same’ responses

VDT_anova_grip_conf_same <- VDT_4T %>%
  filter(stimLev!= 0) %>% filter(resp1_diff ==0) %>% group_by(stimLev, isStrength, sub) %>% mutate(conf_sub = mean(resp2, na.rm = T)) %>%
  ungroup() %>% group_by(stimLev, isStrength) %>% summarise(conf = median(conf_sub, na.rm=T), stimLev, isStrength) %>%
  distinct()
  
head(VDT_anova_grip_conf_same)
aov(conf ~ (stimLev * isStrength), VDT_anova_grip_conf_same) %>% report()
## The ANOVA (formula: conf ~ (stimLev * isStrength)) suggests that:
## 
##   - The main effect of stimLev is statistically significant and large (F(1, 4) =
## 37.32, p = 0.004; Eta2 (partial) = 0.90, 95% CI [0.54, 1.00])
##   - The main effect of isStrength is statistically not significant and large
## (F(1, 4) = 2.74, p = 0.173; Eta2 (partial) = 0.41, 95% CI [0.00, 1.00])
##   - The interaction between stimLev and isStrength is statistically not
## significant and large (F(1, 4) = 6.92, p = 0.058; Eta2 (partial) = 0.63, 95% CI
## [0.00, 1.00])
## 
## Effect sizes were labelled following Field's (2013) recommendations.

6.6 CDT

6.6.1 Specific dataframe

CDT_4T <- lcya_behdata_trial_cdt %>%
  filter(task == 'cdt') %>%
  filter(!is.na(resp1)) %>%
  filter(sub %in% c('13','16','17','18','19','23','24','25','26','27','29','31','33','34','35','36','37','42','43','46','48','54','56','58','61','64','65','68','69','71','72','74','82','83','85','87','88'))

6.6.2 Proportion of Responding ‘Different’

# calculate subject mean
CDT_4T_acc_ws <- CDT_4T %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1_diff, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
CDT_4T_acc_ws <- CDT_4T_acc_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

CDT_4T_acc_ws <- CDT_4T_acc_ws %>%
  mutate(acc_diff = sub_mean- grand_mean) 

# Calculate mean accuracy and standard error for 'Different'
Diff_prop_CDT_4T <- CDT_4T_acc_ws %>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(diff=mean(diff_sub), diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate the proportion of 'Same'
Same_prop_CDT_4T <- CDT_4T_acc_ws %>%
    group_by(sub, stimLev) %>%
    summarise(same_sub = 1-mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(not_diff=mean(same_sub), not_diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

# Combine both datasets for plotting
combined_CDT_4T <- bind_rows(
  mutate(Diff_prop_CDT_4T, condition = "Different", prop = diff),
  mutate(Same_prop_CDT_4T, condition = "Same", prop = not_diff)
)

# Specify the desired order of levels for 'stimLev'
desired_order <- c("0", "5", "20", "45", "90")

# Convert 'stimLev' to factor with the desired order
combined_CDT_4T$stimLev <- factor(combined_CDT_4T$stimLev, levels = desired_order)

# Plotting with the modified 'stimLev'
ggplot(combined_CDT_4T, aes(x = stimLev, y = prop, group = condition, color = condition)) +
  geom_point(size = 2) +
  geom_line(size = 1.2) +
  geom_linerange(mapping = aes(ymin = prop - ifelse(condition == "Different", diff_se, not_diff_se), 
                                ymax = prop + ifelse(condition == "Different", diff_se, not_diff_se))) +
  ylim(0, 1) +
  ggtitle("Proportion of Responding 'Different' and 'Same' - CDT") +
  xlab("Degree") +
  ylab("Proportion") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("#BC3C29FF", "#0072B5FF")) +
  guides(color = guide_legend(title = "condition"))

6.6.2.1 Handgrip manipulation

# calculate subject mean
CDT_4T_acc_ws <- CDT_4T %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1_diff, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
CDT_4T_acc_ws <- CDT_4T_acc_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

CDT_4T_acc_ws <- CDT_4T_acc_ws %>%
  mutate(acc_diff = sub_mean- grand_mean) 

# Calculate mean accuracy and standard error for 'Different'
Diff_prop_high_grip_CDT_4T <- CDT_4T_acc_ws %>%
    filter(isStrength==1)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(diff=mean(diff_sub), diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

Diff_prop_low_grip_CDT_4T <- CDT_4T_acc_ws %>%
    filter(isStrength==0)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(diff=mean(diff_sub), diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate the proportion of 'Same'
Same_prop_high_grip_CDT_4T <- CDT_4T_acc_ws %>%
    filter(isStrength==1) %>%
    group_by(sub, stimLev) %>%
    summarise(same_sub = 1-mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(not_diff=mean(same_sub), not_diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

Same_prop_low_grip_CDT_4T <- CDT_4T_acc_ws %>%
    filter(isStrength==0) %>%
    group_by(sub, stimLev) %>%
    summarise(same_sub = 1-mean(resp1_diff, na.rm = TRUE),acc_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(not_diff=mean(same_sub), not_diff_se = sd(acc_diff)/sqrt(n_distinct(sub))) %>%
    distinct()

# Combine both datasets for plotting
combined_grip_CDT_4T <- bind_rows(
  mutate(Diff_prop_high_grip_CDT_4T, condition = "Different - High Grip", prop = diff),
  mutate(Diff_prop_low_grip_CDT_4T, condition = "Different - Low Grip", prop = diff),
  mutate(Same_prop_high_grip_CDT_4T, condition = "Same - High Grip", prop = not_diff),
  mutate(Same_prop_low_grip_CDT_4T, condition = "Same - Low Grip", prop = not_diff)
)

# Specify the desired order of levels for 'stimLev'
desired_order <- c("0", "5", "20", "45", "90")

# Convert 'stimLev' to factor with the desired order
combined_grip_CDT_4T$stimLev <- factor(combined_grip_CDT_4T$stimLev, levels = desired_order)

# Plotting
ggplot(combined_grip_CDT_4T, aes(x = stimLev, y = prop, group = condition, color = condition)) +
  geom_point( size = 2) +
  geom_line(size = 1.2) +
  geom_linerange(mapping = aes(ymin = prop - ifelse(condition == "Different - High Grip" | condition == "Different - Low Grip", diff_se, not_diff_se), 
                               ymax = prop + ifelse(condition == "Different - High Grip" | condition == "Different - Low Grip", diff_se, not_diff_se)), show.legend = FALSE) +
  ylim(0, 1) +
  ggtitle("Proportion of Responding 'Different' and 'Same' - CDT") +
  xlab("Degree") +
  ylab("Proportion") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("Different - High Grip" = "#BC3C29FF", "Different - Low Grip" = "#ea9999", "Same - High Grip" = "#0072B5FF", "Same - Low Grip" = "#CCE6FF")) +
  scale_linetype_manual(values = c("Different - High Grip" = "solid", "Different - Low Grip" = "solid", "Same - High Grip" = "solid", "Same - Low Grip" = "solid")) +
  guides(color = guide_legend(title = "Response Type", ncol = 1), linetype = "none")

6.6.3 Reaction Time

6.6.3.1 Using Median with within-subject SEM

# calculate subject mean
CDT_4T_RT_ws <- CDT_4T %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1RT, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
CDT_4T_RT_ws <- CDT_4T_RT_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

CDT_4T_RT_ws <- CDT_4T_RT_ws %>%
  mutate(RT_diff = sub_mean- grand_mean) 


# Calculate median RT for different responses
CDT_4T_diff_RT_ws <- CDT_4T_RT_ws %>%
  filter(resp1_diff == 1) %>%
  group_by(stimLev,sub) %>%
  summarise(diff_sub= median(resp1RT), RT_diff) %>%
  ungroup() %>%
  group_by(stimLev) %>%
  summarise(medRT_diff = median(diff_sub),
            medRT_diff_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
  distinct()

# Calculate median RT for same responses
CDT_4T_same_RT_ws <- CDT_4T_RT_ws %>%
  filter(resp1_diff == 0) %>%
  group_by(stimLev,sub) %>%
  summarise(diff_sub= median(resp1RT), RT_diff) %>%
  ungroup() %>%
  group_by(stimLev) %>%
  summarise(medRT_same = median(diff_sub),
            medRT_same_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
  distinct()

# Combine both datasets for plotting
combined_medRT_CDT_4T <- bind_rows(
  mutate(CDT_4T_diff_RT_ws, condition = "Different"),
  mutate(CDT_4T_same_RT_ws, condition = "Same")
)


# Specify the desired order of levels for 'stimLev'
    desired_order <- c("0", "5", "20", "45", "90")

# Convert 'stimLev' to factor with the desired order
combined_medRT_CDT_4T$stimLev <- factor(combined_medRT_CDT_4T$stimLev, levels = desired_order)

#Sort the combined dataset by stimLev within each condition
combined_medRT_CDT_4T <- combined_medRT_CDT_4T %>%
  arrange(condition, stimLev)

# Plotting
ggplot(combined_medRT_CDT_4T, aes(x = stimLev, group = condition, color = condition, linetype= condition)) +
  geom_point(aes(y = ifelse(condition == "Different", medRT_diff , medRT_same)), size = 2) +
  geom_line(aes(y = ifelse(condition == "Different", medRT_diff, medRT_same)), size = 1.2) +
  ylim(min(c(combined_medRT_CDT_4T$medRT_diff, combined_medRT_CDT_4T$medRT_same)), max(c(combined_medRT_CDT_4T$medRT_diff, combined_medRT_CDT_4T$medRT_same))) +
  ggtitle("Median of Reaction Time for Same and Different Responses - CDT") +
  geom_linerange(mapping = aes(ymin = medRT_diff - medRT_diff_se, ymax = medRT_diff + medRT_diff_se, linetype = condition), data = filter(combined_medRT_CDT_4T, condition == "Different")) +
  geom_linerange(mapping = aes(ymin = medRT_same - medRT_same_se, ymax = medRT_same + medRT_same_se, linetype = condition), data = filter(combined_medRT_CDT_4T, condition == "Same")) +
  labs(caption = "Error bars represent within-subject SEM") +
  xlab("Degree") +
  ylab("Reaction Time (S)") +
  scale_color_manual(values = c( "#BC3C29FF", "#0072B5FF")) +
  scale_linetype_manual(values = c("Different" = "solid", "Same" = "solid")) 

6.6.3.2 Using Median within and Mean between subjects with WSSEM

# calculate subject mean
CDT_4T_RT_ws <- CDT_4T %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1RT, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
CDT_4T_RT_ws <- CDT_4T_RT_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

CDT_4T_RT_ws <- CDT_4T_RT_ws %>%
  mutate(RT_diff = sub_mean- grand_mean) 


# Calculate median RT for different responses
CDT_4T_diff_RT_ws <- CDT_4T_RT_ws %>%
  filter(resp1_diff == 1) %>%
  group_by(stimLev,sub) %>%
  summarise(diff_sub= median(resp1RT), RT_diff) %>%
  ungroup() %>%
  group_by(stimLev) %>%
  summarise(medRT_diff = mean(diff_sub, na.rm = T),
            medRT_diff_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
  distinct()

# Calculate median RT for same responses
CDT_4T_same_RT_ws <- CDT_4T_RT_ws %>%
  filter(resp1_diff == 0) %>%
  group_by(stimLev,sub) %>%
  summarise(diff_sub= median(resp1RT), RT_diff) %>%
  ungroup() %>%
  group_by(stimLev) %>%
  summarise(medRT_same = mean(diff_sub, na.rm = T),
            medRT_same_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
  distinct()

# Combine both datasets for plotting
combined_RT_CDT_4T <- bind_rows(
  mutate(CDT_4T_diff_RT_ws, condition = "Different"),
  mutate(CDT_4T_same_RT_ws, condition = "Same")
)


# Specify the desired order of levels for 'stimLev'
    desired_order <- c("0", "5", "20", "45", "90")

# Convert 'stimLev' to factor with the desired order
combined_RT_CDT_4T$stimLev <- factor(combined_RT_CDT_4T$stimLev, levels = desired_order)

#Sort the combined dataset by stimLev within each condition
combined_RT_CDT_4T <- combined_RT_CDT_4T %>%
  arrange(condition, stimLev)

# Plotting
ggplot(combined_RT_CDT_4T, aes(x = stimLev, group = condition, color = condition, linetype= condition)) +
  geom_point(aes(y = ifelse(condition == "Different", medRT_diff , medRT_same)), size = 2) +
  geom_line(aes(y = ifelse(condition == "Different", medRT_diff, medRT_same)), size = 1.2) +
  ylim(min(c(combined_RT_CDT_4T$medRT_diff, combined_RT_CDT_4T$medRT_same)), max(c(combined_RT_CDT_4T$medRT_diff, combined_RT_CDT_4T$medRT_same))) +
  ggtitle("Median of Within ane Mean Within Reaction Time - CDT") +
  geom_linerange(mapping = aes(ymin = medRT_diff - medRT_diff_se, ymax = medRT_diff + medRT_diff_se, linetype = condition), data = filter(combined_RT_CDT_4T, condition == "Different")) +
  geom_linerange(mapping = aes(ymin = medRT_same - medRT_same_se, ymax = medRT_same + medRT_same_se, linetype = condition), data = filter(combined_RT_CDT_4T, condition == "Same")) +
  labs(caption = "Error bars represent within-subject SEM") +
  xlab("Degree") +
  ylab("Reaction Time (S)") +
  scale_color_manual(values = c( "#BC3C29FF", "#0072B5FF")) +
  scale_linetype_manual(values = c("Different" = "solid", "Same" = "solid")) 

6.6.3.3 Handgrip manipulation with Median

# calculate subject mean
CDT_4T_RT_ws <- CDT_4T %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1RT, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calcualte the grand mean
CDT_4T_RT_ws <- CDT_4T_RT_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

CDT_4T_RT_ws <- CDT_4T_RT_ws %>%
  mutate(RT_diff = sub_mean- grand_mean) 

# Calculate mean RT and standard error for  High 'Different'
RT_diff_high_grip_CDT_4T <- CDT_4T_RT_ws %>%
    filter(resp1_diff == 1) %>%
    filter(isStrength==1)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_high_diff=median(diff_sub), medRT_high_diff_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate mean RT and standard error for  Low 'Different'
RT_diff_low_grip_CDT_4T <- CDT_4T_RT_ws %>%
    filter(resp1_diff == 1) %>%
    filter(isStrength==0)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_low_diff=median(diff_sub), medRT_low_diff_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()


# Calculate mean RT and standard error for  High 'Same'
RT_same_high_grip_CDT_4T <- CDT_4T_RT_ws %>%
    filter(resp1_diff == 0) %>%
    filter(isStrength==1)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_high_same=median(diff_sub), medRT_high_same_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate mean RT and standard error for  Low 'Same'
RT_same_low_grip_CDT_4T <- CDT_4T_RT_ws %>%
    filter(resp1_diff == 0) %>%
    filter(isStrength==0)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_low_same=median(diff_sub), medRT_low_same_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()


# Combine both datasets for plotting
combined_medRT_grip_CDT_4T <- bind_rows(
  mutate(RT_diff_high_grip_CDT_4T, condition = "Different - High Grip"),
  mutate(RT_diff_low_grip_CDT_4T, condition = "Different - Low Grip"),
  mutate(RT_same_high_grip_CDT_4T, condition = "Same - High Grip"),
  mutate(RT_same_low_grip_CDT_4T, condition = "Same - Low Grip")
)

# Specify the desired order of levels for 'stimLev'
    desired_order <- c("0", "5", "20", "45", "90")

# Convert 'stimLev' to factor with the desired order
combined_medRT_grip_CDT_4T$stimLev <- factor(combined_medRT_grip_CDT_4T$stimLev, levels = desired_order)


# Plotting without jitter
ggplot(data = combined_medRT_grip_CDT_4T) +
  geom_point(aes(x = stimLev, y = medRT_high_diff, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_high_diff, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_high_diff - medRT_high_diff_se, 
                     ymax = medRT_high_diff + medRT_high_diff_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_low_diff, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_low_diff, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_low_diff - medRT_low_diff_se, 
                     ymax = medRT_low_diff + medRT_low_diff_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_high_same, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_high_same, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_high_same - medRT_high_same_se, 
                     ymax = medRT_high_same + medRT_high_same_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_low_same, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_low_same, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_low_same - medRT_low_same_se, 
                     ymax = medRT_low_same + medRT_low_same_se, color = condition), 
                 linetype = "solid") +
  
  ggtitle("Median of Reaction Time for Same and Different Responses - CDT") +
  xlab("Degree") +
  ylab("Median Reaction Time") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("#BC3C29FF", "#ea9999", "#0072B5FF", "#CCE6FF"),
                      name = "Condition") 

6.6.3.4 Handgrip manipulation with Median within and Mean between with WSSEM

# calculate subject mean
CDT_4T_RT_ws <- CDT_4T %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp1RT, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calcualte the grand mean
CDT_4T_RT_ws <- CDT_4T_RT_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

CDT_4T_RT_ws <- CDT_4T_RT_ws %>%
  mutate(RT_diff = sub_mean- grand_mean) 

# Calculate mean RT and standard error for  High 'Different'
RT_diff_high_grip_CDT_4T <- CDT_4T_RT_ws %>%
    filter(resp1_diff == 1) %>%
    filter(isStrength==1)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_high_diff=mean(diff_sub, na.rm = T), medRT_high_diff_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate mean RT and standard error for  Low 'Different'
RT_diff_low_grip_CDT_4T <- CDT_4T_RT_ws %>%
    filter(resp1_diff == 1) %>%
    filter(isStrength==0)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_low_diff=mean(diff_sub, na.rm = T), medRT_low_diff_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()


# Calculate mean RT and standard error for  High 'Same'
RT_same_high_grip_CDT_4T <- CDT_4T_RT_ws %>%
    filter(resp1_diff == 0) %>%
    filter(isStrength==1)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_high_same=mean(diff_sub, na.rm = T), medRT_high_same_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()

# Calculate mean RT and standard error for  Low 'Same'
RT_same_low_grip_CDT_4T <- CDT_4T_RT_ws %>%
    filter(resp1_diff == 0) %>%
    filter(isStrength==0)%>%
    group_by(sub, stimLev) %>%
    summarise(diff_sub = median(resp1RT),RT_diff) %>%
    ungroup() %>% 
    group_by(stimLev) %>%
    summarise(medRT_low_same=mean(diff_sub, na.rm = T), medRT_low_same_se = sd(RT_diff, na.rm=T)/sqrt(n_distinct(sub))) %>%
    distinct()


# Combine both datasets for plotting
combined_RT_grip_CDT_4T <- bind_rows(
  mutate(RT_diff_high_grip_CDT_4T, condition = "Different - High Grip"),
  mutate(RT_diff_low_grip_CDT_4T, condition = "Different - Low Grip"),
  mutate(RT_same_high_grip_CDT_4T, condition = "Same - High Grip"),
  mutate(RT_same_low_grip_CDT_4T, condition = "Same - Low Grip")
)

# Specify the desired order of levels for 'stimLev'
    desired_order <- c("0", "5", "20", "45", "90")

# Convert 'stimLev' to factor with the desired order
combined_RT_grip_CDT_4T$stimLev <- factor(combined_RT_grip_CDT_4T$stimLev, levels = desired_order)


# Plotting without jitter
ggplot(data = combined_RT_grip_CDT_4T) +
  geom_point(aes(x = stimLev, y = medRT_high_diff, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_high_diff, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_high_diff - medRT_high_diff_se, 
                     ymax = medRT_high_diff + medRT_high_diff_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_low_diff, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_low_diff, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_low_diff - medRT_low_diff_se, 
                     ymax = medRT_low_diff + medRT_low_diff_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_high_same, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_high_same, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_high_same - medRT_high_same_se, 
                     ymax = medRT_high_same + medRT_high_same_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimLev, y = medRT_low_same, color = condition), size = 2) +
  geom_line(aes(x = stimLev, y = medRT_low_same, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimLev, ymin = medRT_low_same - medRT_low_same_se, 
                     ymax = medRT_low_same + medRT_low_same_se, color = condition), 
                 linetype = "solid") +
  
  ggtitle("Median of Reaction Time Within and Mean Between - CDT") +
  xlab("Degree") +
  ylab("Median Reaction Time") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("#BC3C29FF", "#ea9999", "#0072B5FF", "#CCE6FF"),
                      name = "Condition") 

6.6.4 Confidence

# calculate subject mean
CDT_4T_conf_ws <- CDT_4T %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp2, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
CDT_4T_conf_ws <- CDT_4T_conf_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

CDT_4T_conf_ws <- CDT_4T_conf_ws %>%
  mutate(conf_diff = sub_mean- grand_mean) 

diff_average_confidence_CDT_4T <- CDT_4T_conf_ws %>%
        filter(resp1_diff== 1) %>%
        group_by(stimLev, sub) %>%
        summarise(diff_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(diff_conf = mean(diff_prop, na.rm = T), 
                  diff_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()

same_average_confidence_CDT_4T <- CDT_4T_conf_ws %>%
        filter(resp1_diff== 0) %>%
        group_by(stimLev, sub) %>%
        summarise(same_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(same_conf = mean(same_prop), 
                  same_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()

  combined_prop_CDT_4T <- bind_rows(
        mutate(diff_average_confidence_CDT_4T, condition = "Different"),
        mutate(same_average_confidence_CDT_4T, condition = "Same")
    )
  
    # Specify the desired order of levels for 'stimLev'
    desired_order <- c("0", "5", "20", "45", "90")
    
    # Convert 'stimLev' to factor with the desired order
    combined_prop_CDT_4T$stimLev <- factor(combined_prop_CDT_4T$stimLev, levels = desired_order)
    
    # Sort the combined dataset by stimLev within each condition
    combined_prop_CDT_4T <- combined_prop_CDT_4T %>%
        arrange(condition, stimLev)
    
    # Plotting
    ggplot(combined_prop_CDT_4T, aes(x = stimLev, group = condition, color = condition)) +
        geom_point(aes(y = ifelse(condition == "Different", diff_conf, same_conf)), size = 2) +
    geom_line(aes(y = ifelse(condition == "Different", diff_conf, same_conf)), size = 1.2) +
        ylim(2, 4) +
        ggtitle("Average of Confidence Response - CDT") +
        geom_linerange(mapping = aes(ymin = diff_conf - diff_conf_se, ymax = diff_conf + diff_conf_se, linetype = condition), data = filter(combined_prop_CDT_4T, condition == "Different")) +
    geom_linerange(mapping = aes(ymin = same_conf - same_conf_se, ymax = same_conf + same_conf_se, linetype = condition), data = filter(combined_prop_CDT_4T, condition == "Same")) +
        labs(caption = "Error bars represent within-subject SEM") +
        xlab("Degree") +
        ylab("Confidence Rating") +
        scale_color_manual(values = c("Different" = "#BC3C29FF",
                                      "Same" = "#0072B5FF"
                                      ),
                           labels = c("Different",
                                      "Same"
                                      )) +
        scale_linetype_manual(values = c("Different" = "solid", "Same" = "solid"))

6.6.4.1 Handgrip manipulation

# calculate subject mean
CDT_4T_conf_ws <- CDT_4T %>%
  group_by(sub, stimLev, resp1_diff) %>%
  mutate(sub_mean = mean(resp2, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
CDT_4T_conf_ws <- CDT_4T_conf_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

CDT_4T_conf_ws <- CDT_4T_conf_ws %>%
  mutate(conf_diff = sub_mean- grand_mean) 

diff_average_high_confidence_CDT_4T <- CDT_4T_conf_ws %>%
        filter(isStrength == 1) %>%
        filter(resp1_diff== 1) %>%
        group_by(stimLev, sub) %>%
        summarise(diff_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(diff_high_conf = mean(diff_prop, na.rm = T), 
                  diff_high_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()

diff_average_low_confidence_CDT_4T <- CDT_4T_conf_ws %>%
        filter(isStrength == 0) %>%
        filter(resp1_diff== 1) %>%
        group_by(stimLev, sub) %>%
        summarise(diff_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(diff_low_conf = mean(diff_prop, na.rm = T), 
                  diff_low_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()


same_average_high_confidence_CDT_4T <- CDT_4T_conf_ws %>%
        filter(isStrength == 1) %>%
        filter(resp1_diff== 0) %>%
        group_by(stimLev, sub) %>%
        summarise(same_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(same_high_conf = mean(same_prop, na.rm = T), 
                  same_high_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()

same_average_low_confidence_CDT_4T <- CDT_4T_conf_ws %>%
        filter(isStrength == 0) %>%
        filter(resp1_diff== 0) %>%
        group_by(stimLev, sub) %>%
        summarise(same_prop = mean(resp2, na.rm = TRUE),conf_diff) %>%
        ungroup() %>%
        group_by(stimLev) %>%
        summarise(same_low_conf = mean(same_prop, na.rm = T), 
                  same_low_conf_se = sd(conf_diff, na.rm=T) / sqrt(n_distinct(sub))) %>%
        distinct()


combined_conf_grip_CDT_4T <- bind_rows(
        mutate(diff_average_high_confidence_CDT_4T, condition = "Different - High Grip"),
        mutate(diff_average_low_confidence_CDT_4T, condition = "Different - Low Grip" ),
        mutate(same_average_high_confidence_CDT_4T, condition = "Same - High Grip"),
        mutate(same_average_low_confidence_CDT_4T, condition = "Same - Low Grip")
)
  
    # Specify the desired order of levels for 'stimLev'
    desired_order <- c("0", "5", "20", "45", "90")
    
    # Convert 'stimLev' to factor with the desired order
    combined_conf_grip_CDT_4T$stimLev <- factor(combined_conf_grip_CDT_4T$stimLev, levels = desired_order)
    
    # Sort the combined dataset by stimLev within each condition
    combined_conf_grip_CDT_4T <- combined_conf_grip_CDT_4T %>%
        arrange(condition, stimLev)
    


ggplot(combined_conf_grip_CDT_4T, aes(x = stimLev, color = condition)) +
  geom_point(aes(y = diff_high_conf), size = 3) +
  geom_line(aes(y = diff_high_conf, group = condition), size = 1.2) +
  geom_linerange(aes(ymin = diff_high_conf - diff_high_conf_se, 
                     ymax = diff_high_conf + diff_high_conf_se, group = condition), 
                 width = 0.2, linetype = "solid") +
  geom_point(aes(y = diff_low_conf), size = 3) +
  geom_line(aes(y = diff_low_conf, group = condition), size = 1.2, linetype = "solid") +
  geom_linerange(aes(ymin = diff_low_conf - diff_low_conf_se, 
                     ymax = diff_low_conf + diff_low_conf_se, group = condition), 
                 width = 0.2, linetype = "solid") +
  geom_point(aes(y = same_high_conf), size = 3) +
  geom_line(aes(y = same_high_conf, group = condition), size = 1.2) +
  geom_linerange(aes(ymin = same_high_conf - same_high_conf_se, 
                     ymax = same_high_conf + same_high_conf_se, group = condition), 
                  linetype = "solid") +
  geom_point(aes(y = same_low_conf), size = 3) +
  geom_line(aes(y = same_low_conf, group = condition), size = 1.2, linetype = "solid") +
  geom_linerange(aes(ymin = same_low_conf - same_low_conf_se, 
                     ymax = same_low_conf + same_low_conf_se, group = condition), 
                 width = 0.2, linetype = "solid") +
  ggtitle("Average of Confidence Response - CDT") +
  labs(caption = "Error bars represent within-subject SEM", x = "Degree", y = "Confidence Rating") +
  scale_color_manual(values = c("#BC3C29FF", "#ea9999", "#0072B5FF", "#CCE6FF"),
                     name = "Response Type") +
  ylim(2,4)+
  scale_linetype_manual(values = c("solid", "solid"), name = "Condition",
                        labels = c("High Grip", "Low Grip")) 

6.7 Statistical Analysis

6.7.1 Effect of Grip on responses

6.7.1.1 The Effect of Grip on ‘Different’ Responses across Stimulus Levels

CDT_anova_grip_diff <- CDT_4T %>%
  filter(stimLev!= 0) %>% group_by(stimLev, isStrength, sub) %>% mutate(diff_sub = mean(resp1_diff, na.rm = TRUE)) %>%
  ungroup() %>% group_by(stimLev, isStrength) %>% summarise(diff = mean(diff_sub), stimLev, isStrength) %>%
  distinct()
  
head(CDT_anova_grip_diff)
aov(diff ~ (stimLev * isStrength), CDT_anova_grip_diff) %>% report()
## The ANOVA (formula: diff ~ (stimLev * isStrength)) suggests that:
## 
##   - The main effect of stimLev is statistically significant and large (F(1, 4) =
## 40.29, p = 0.003; Eta2 (partial) = 0.91, 95% CI [0.56, 1.00])
##   - The main effect of isStrength is statistically not significant and small
## (F(1, 4) = 0.17, p = 0.702; Eta2 (partial) = 0.04, 95% CI [0.00, 1.00])
##   - The interaction between stimLev and isStrength is statistically not
## significant and very small (F(1, 4) = 0.03, p = 0.871; Eta2 (partial) =
## 7.47e-03, 95% CI [0.00, 1.00])
## 
## Effect sizes were labelled following Field's (2013) recommendations.

6.7.1.2 The Effect of Grip on ‘Same’ Responses across Stimulus Levels

CDT_anova_grip_same <- CDT_4T %>%
  filter(stimLev!= 0) %>% group_by(stimLev, isStrength, sub) %>% mutate(diff_sub = 1-mean(resp1_diff, na.rm = TRUE)) %>%
  ungroup() %>% group_by(stimLev, isStrength) %>% summarise(diff = mean(diff_sub, na.rm = T), stimLev, isStrength) %>%
  distinct()
  
head(CDT_anova_grip_same)
aov(diff ~ (stimLev * isStrength), CDT_anova_grip_same) %>% report()
## The ANOVA (formula: diff ~ (stimLev * isStrength)) suggests that:
## 
##   - The main effect of stimLev is statistically significant and large (F(1, 4) =
## 40.29, p = 0.003; Eta2 (partial) = 0.91, 95% CI [0.56, 1.00])
##   - The main effect of isStrength is statistically not significant and small
## (F(1, 4) = 0.17, p = 0.702; Eta2 (partial) = 0.04, 95% CI [0.00, 1.00])
##   - The interaction between stimLev and isStrength is statistically not
## significant and very small (F(1, 4) = 0.03, p = 0.871; Eta2 (partial) =
## 7.47e-03, 95% CI [0.00, 1.00])
## 
## Effect sizes were labelled following Field's (2013) recommendations.

6.7.2 Effect of Grip on Reaction Time

6.7.2.1 The Effect of Grip on Median Reaction Time for ‘Different’ responses

CDT_anova_grip_RT_diff <- CDT_4T %>%
  filter(stimLev!= 0) %>% filter(resp1_diff ==1) %>% group_by(stimLev, isStrength, sub) %>% mutate(RT_sub = median(resp1RT)) %>%
  ungroup() %>% group_by(stimLev, isStrength) %>% summarise(RT = median(RT_sub), stimLev, isStrength) %>%
  distinct()
  
head(CDT_anova_grip_RT_diff)
aov(RT ~ (stimLev * isStrength), CDT_anova_grip_RT_diff) %>% report()
## The ANOVA (formula: RT ~ (stimLev * isStrength)) suggests that:
## 
##   - The main effect of stimLev is statistically significant and large (F(1, 4) =
## 16.96, p = 0.015; Eta2 (partial) = 0.81, 95% CI [0.25, 1.00])
##   - The main effect of isStrength is statistically not significant and medium
## (F(1, 4) = 0.40, p = 0.559; Eta2 (partial) = 0.09, 95% CI [0.00, 1.00])
##   - The interaction between stimLev and isStrength is statistically not
## significant and medium (F(1, 4) = 0.30, p = 0.611; Eta2 (partial) = 0.07, 95%
## CI [0.00, 1.00])
## 
## Effect sizes were labelled following Field's (2013) recommendations.

6.7.2.2 The Effect of Grip on Median Reaction Time for ‘Same’ responses

CDT_anova_grip_RT_same <- CDT_4T %>%
  filter(stimLev!= 0) %>% filter(resp1_diff ==0) %>% group_by(stimLev, isStrength, sub) %>% mutate(RT_sub = median(resp1RT)) %>%
  ungroup() %>% group_by(stimLev, isStrength) %>% summarise(RT = median(RT_sub), stimLev, isStrength) %>%
  distinct()
  
head(CDT_anova_grip_RT_same)
aov(RT ~ (stimLev * isStrength), CDT_anova_grip_RT_same) %>% report()
## The ANOVA (formula: RT ~ (stimLev * isStrength)) suggests that:
## 
##   - The main effect of stimLev is statistically not significant and large (F(1,
## 4) = 3.99, p = 0.116; Eta2 (partial) = 0.50, 95% CI [0.00, 1.00])
##   - The main effect of isStrength is statistically not significant and large
## (F(1, 4) = 0.74, p = 0.440; Eta2 (partial) = 0.16, 95% CI [0.00, 1.00])
##   - The interaction between stimLev and isStrength is statistically not
## significant and large (F(1, 4) = 1.12, p = 0.349; Eta2 (partial) = 0.22, 95% CI
## [0.00, 1.00])
## 
## Effect sizes were labelled following Field's (2013) recommendations.

6.7.3 Effect of Grip on Confidence

6.7.3.1 The Effect of Grip on Confidence Reports for ‘Different’ responses

CDT_anova_grip_conf_diff <- CDT_4T %>%
  filter(stimLev!= 0) %>% filter(resp1_diff ==1) %>% group_by(stimLev, isStrength, sub) %>% mutate(conf_sub = mean(resp2, na.rm = T)) %>%
  ungroup() %>% group_by(stimLev, isStrength) %>% summarise(conf = mean(conf_sub, na.rm = T), stimLev, isStrength) %>%
  distinct()
  
head(CDT_anova_grip_conf_diff)
aov(conf ~ (stimLev * isStrength), CDT_anova_grip_conf_diff) %>% report()
## The ANOVA (formula: conf ~ (stimLev * isStrength)) suggests that:
## 
##   - The main effect of stimLev is statistically significant and large (F(1, 4) =
## 58.38, p = 0.002; Eta2 (partial) = 0.94, 95% CI [0.67, 1.00])
##   - The main effect of isStrength is statistically not significant and medium
## (F(1, 4) = 0.58, p = 0.488; Eta2 (partial) = 0.13, 95% CI [0.00, 1.00])
##   - The interaction between stimLev and isStrength is statistically not
## significant and medium (F(1, 4) = 0.35, p = 0.586; Eta2 (partial) = 0.08, 95%
## CI [0.00, 1.00])
## 
## Effect sizes were labelled following Field's (2013) recommendations.

6.7.3.2 The Effect of Grip on Confidence Reports for ‘Same’ responses

CDT_anova_grip_conf_same <- CDT_4T %>%
  filter(stimLev!= 0) %>% filter(resp1_diff ==0) %>% group_by(stimLev, isStrength, sub) %>% mutate(conf_sub = mean(resp2, na.rm = T)) %>%
  ungroup() %>% group_by(stimLev, isStrength) %>% summarise(conf = median(conf_sub, na.rm=T), stimLev, isStrength) %>%
  distinct()
  
head(CDT_anova_grip_conf_same)
aov(conf ~ (stimLev * isStrength), CDT_anova_grip_conf_same) %>% report()
## The ANOVA (formula: conf ~ (stimLev * isStrength)) suggests that:
## 
##   - The main effect of stimLev is statistically significant and large (F(1, 4) =
## 98.96, p < .001; Eta2 (partial) = 0.96, 95% CI [0.79, 1.00])
##   - The main effect of isStrength is statistically not significant and small
## (F(1, 4) = 0.17, p = 0.700; Eta2 (partial) = 0.04, 95% CI [0.00, 1.00])
##   - The interaction between stimLev and isStrength is statistically significant
## and large (F(1, 4) = 28.46, p = 0.006; Eta2 (partial) = 0.88, 95% CI [0.44,
## 1.00])
## 
## Effect sizes were labelled following Field's (2013) recommendations.

6.8 MST

6.8.1 Specific Dataframes

MST_4T <- lcya_behdata_trial_mst %>%
    filter(task == 'MSTtest') %>%
    filter(!is.na(resp1)) %>%
    filter(sub %in% c('13','16','17','18','19','23','24','25','26','27','29','31','33','34','35','36','37','42','43','46','48','54','56','58','61','64','65','68','69','71','72','74','82','83','85','87','88'))


# Read the text file with im_num and lure_bin values
SetC_4 <- read.table("SetC_4.txt", header=F)

colnames(SetC_4)[1] <- "im_num"
colnames(SetC_4)[2] <- "new_lure_bin"
MST_4T <- left_join(MST_4T, SetC_4, by="im_num")

MST_4T$lure_bin <- ifelse(!is.na(MST_4T$new_lure_bin), MST_4T$new_lure_bin, MST_4T$lure_bin)

# Drop the new_lure_bin column if you don't need it anymore
MST_4T <- subset(MST_4T, select = -c(new_lure_bin))

MST_4T <- MST_4T %>%
  mutate(lure_bin = ifelse(trial_type != "lure", NA, lure_bin))


# Update stimlev_mst values based on the specified rules
MST_4T <- MST_4T %>%
  mutate(stimlev_mst = case_when(
    trial_type == "foil" ~ "5foil",
    trial_type == "targ" ~ "0targ",
    lure_bin == '1' ~ "1lure",
    lure_bin == '2' ~ "2lure",
    lure_bin == '3' ~ "3lure",
    lure_bin == '4' ~ "4lure",
    TRUE ~ as.character(stimlev_mst)  # Keep the original value if none of the conditions are met
  ))

6.8.2 Proportion of Responding ‘Old’, ‘Similar’, ‘New’

# calculate subject mean
    MST_4T_acc_ws <- MST_4T %>%
      group_by(sub, stimlev_mst, resp1_diff) %>%
      mutate(sub_mean = mean(resp1_diff, na.rm=T)) %>%
      distinct() %>%
      ungroup()

# calculate the grand mean
    MST_4T_acc_ws <- MST_4T_acc_ws %>%
      group_by(sub) %>%
      mutate(grand_mean = mean(sub_mean)) %>%
      ungroup()

# subtract the subject mean and add grand mean for each cell

    MST_4T_acc_ws <- MST_4T_acc_ws %>%
      mutate(acc_diff = sub_mean- grand_mean) 

# Calculate mean proportion for old responses
    Old_prop_MST_4T <- MST_4T_acc_ws %>%
      group_by(stimlev_mst, sub) %>%
      mutate(resp1_old = case_when(resp1 == 'old' ~ 1, TRUE ~ 0)) %>%  # Corrected the syntax for case_when
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(prop_old = mean(resp1_old, na.rm = T),
                prop_old_se = sd(acc_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
        
# Calculate mean proportion for sim responses
    Sim_prop_MST_4T <- MST_4T_acc_ws %>%
      group_by(stimlev_mst, sub) %>%
      mutate(resp1_sim = case_when(resp1 == 'sim' ~ 1, TRUE ~ 0)) %>%  # Corrected the syntax for case_when
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(prop_sim = mean(resp1_sim, na.rm = T),
                prop_sim_se = sd(acc_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
    
# Calculate mean proportion for new responses
    New_prop_MST_4T <- MST_4T_acc_ws %>%
      group_by(stimlev_mst, sub) %>%
      mutate(resp1_new = case_when(resp1 == 'new' ~ 1, TRUE ~ 0)) %>%  # Corrected the syntax for case_when
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(prop_new = mean(resp1_new, na.rm = T),
                prop_new_se = sd(acc_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
    
# Combine both datasets for plotting
     combined_prop_MST_4T <- bind_rows(
        mutate(Old_prop_MST_4T, condition = "Old"),
        mutate(Sim_prop_MST_4T, condition = "Similar"),
        mutate(New_prop_MST_4T, condition = "New")
    )
    combined_prop_MST_4T$stimlev_mst <- factor(combined_prop_MST_4T$stimlev_mst, levels = desired_order)

# Specify the desired order of levels for 'stimlev_mst'
    desired_order <- c("0targ", "1lure", "2lure", "3lure", "4lure", "5foil")

# Convert 'stimlev_mst' to factor with the desired order
  combined_prop_MST_4T$stimlev_mst <- factor(combined_prop_MST_4T$stimlev_mst, levels = desired_order)



# Plotting
ggplot(combined_prop_MST_4T, aes(x = stimlev_mst)) +
  geom_line(aes(y = prop_old, group = condition, color = "Old"), size = 1.2) +
  geom_line(aes(y = prop_sim, group = condition, color = "Similar"), size = 1.2) +
  geom_line(aes(y = prop_new, group = condition, color = "New"), size = 1.2) +
  geom_point(aes(y = prop_old, color = "Old"), size = 2) +
  geom_point(aes(y = prop_sim, color = "Similar"), size = 2) +
  geom_point(aes(y = prop_new, color = "New"), size = 2) +
  geom_linerange(aes(ymin = prop_old - prop_old_se, ymax = prop_old + prop_old_se), color = "#0072B5FF", size = 0.5) +
  geom_linerange(aes(ymin = prop_sim - prop_sim_se, ymax = prop_sim + prop_sim_se), color = "#20854EFF", size = 0.5) +
  geom_linerange(aes(ymin = prop_new - prop_new_se, ymax = prop_new + prop_new_se), color = "#BC3C29FF", size = 0.5) +
  ggtitle("Proportions of Old, Similar, and New Responses - MST") +
  xlab("Stimulus Level") +
  ylab("Proportion") +
  ylim(0,1) +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values=c("Old"="#0072B5FF", "New"="#BC3C29FF", "Similar"="#20854EFF")) +
  guides(color = guide_legend(title = "Response Type"))

6.8.2.1 Handgrip manipulation

# calculate subject mean
    MST_4T_acc_ws <- MST_4T %>%
      group_by(sub, stimlev_mst, resp1_diff) %>%
      mutate(sub_mean = mean(resp1_diff, na.rm=T)) %>%
      distinct() %>%
      ungroup()

# calculate the grand mean
    MST_4T_acc_ws <- MST_4T_acc_ws %>%
      group_by(sub) %>%
      mutate(grand_mean = mean(sub_mean)) %>%
      ungroup()

# subtract the subject mean and add grand mean for each cell

    MST_4T_acc_ws <- MST_4T_acc_ws %>%
      mutate(acc_diff = sub_mean- grand_mean) 

# Calculate mean proportion for high - old responses
    Old_prop_high_grip_MST_4T <- MST_4T_acc_ws %>%
      filter(isStrength==1) %>%
      group_by(stimlev_mst, sub) %>%
      mutate(resp1_old = case_when(resp1 == 'old' ~ 1, TRUE ~ 0)) %>% 
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(prop_old_high = mean(resp1_old, na.rm = T),
                prop_old_high_se = sd(acc_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()

# Calculate mean proportion for low - old responses
    Old_prop_low_grip_MST_4T <- MST_4T_acc_ws %>%
      filter(isStrength==0) %>%
      group_by(stimlev_mst, sub) %>%
      mutate(resp1_old = case_when(resp1 == 'old' ~ 1, TRUE ~ 0)) %>% 
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(prop_old_low = mean(resp1_old, na.rm = T),
                prop_old_low_se = sd(acc_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()    
    
# Calculate mean proportion for high-sim responses
    Sim_prop_high_grip_MST_4T <- MST_4T_acc_ws %>%
      filter(isStrength==1) %>%
      group_by(stimlev_mst, sub) %>%
      mutate(resp1_sim = case_when(resp1 == 'sim' ~ 1, TRUE ~ 0)) %>%  
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(prop_sim_high = mean(resp1_sim, na.rm = T),
                prop_sim_high_se = sd(acc_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
    
# Calculate mean proportion for low-sim responses
    Sim_prop_low_grip_MST_4T <- MST_4T_acc_ws %>%
      filter(isStrength==0) %>%
      group_by(stimlev_mst, sub) %>%
      mutate(resp1_sim = case_when(resp1 == 'sim' ~ 1, TRUE ~ 0)) %>%  
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(prop_sim_low = mean(resp1_sim, na.rm = T),
                prop_sim_low_se = sd(acc_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
        
# Calculate mean proportion for high-new responses
    New_prop_high_grip_MST_4T <- MST_4T_acc_ws %>%
      filter(isStrength==1) %>%
      group_by(stimlev_mst, sub) %>%
      mutate(resp1_new = case_when(resp1 == 'new' ~ 1, TRUE ~ 0)) %>%  
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(prop_new_high = mean(resp1_new, na.rm = T),
                prop_new_high_se = sd(acc_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
   
# Calculate mean proportion for low-new responses
    New_prop_low_grip_MST_4T <- MST_4T_acc_ws %>%
      filter(isStrength==0) %>%
      group_by(stimlev_mst, sub) %>%
      mutate(resp1_new = case_when(resp1 == 'new' ~ 1, TRUE ~ 0)) %>%  
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(prop_new_low = mean(resp1_new, na.rm = T),
                prop_new_low_se = sd(acc_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
    
    
# Combine both datasets for plotting
combined_grip_MST_4T <- bind_rows(
  mutate(Old_prop_high_grip_MST_4T, condition = "Old - High Grip", prop = prop_old_high),
  mutate(Old_prop_low_grip_MST_4T, condition = "Old - Low Grip", prop = prop_old_low),
  mutate(Sim_prop_high_grip_MST_4T, condition = "Similar - High Grip", prop = prop_sim_high),
  mutate(Sim_prop_low_grip_MST_4T, condition = "Similar - Low Grip", prop = prop_sim_low),
  mutate(New_prop_high_grip_MST_4T, condition = "New - High Grip", prop = prop_new_high),
  mutate(New_prop_low_grip_MST_4T, condition = "New - Low Grip", prop = prop_new_low)
)

  combined_prop_MST_4T$stimlev_mst <- factor(combined_prop_MST_4T$stimlev_mst, levels = desired_order)

# Specify the desired order of levels for 'stimlev_mst'
    desired_order <- c("0targ", "1lure", "2lure", "3lure", "4lure", "5foil")

# Convert 'stimlev_mst' to factor with the desired order
  combined_prop_MST_4T$stimlev_mst <- factor(combined_prop_MST_4T$stimlev_mst, levels = desired_order)



# Plotting
ggplot(combined_grip_MST_4T, aes(x = stimlev_mst, y = prop, group = condition, color = condition)) +
  geom_point(size = 2) +
  geom_line(size = 1.2) +
  geom_linerange(mapping = aes(ymin = prop - ifelse(grepl("Old", condition), prop_old_high_se, ifelse(grepl("Similar", condition), prop_sim_high_se, prop_new_high_se)),
                                  ymax = prop + ifelse(grepl("Old", condition), prop_old_high_se, ifelse(grepl("Similar", condition), prop_sim_high_se, prop_new_high_se)),
                                  color = condition),
                    show.legend = FALSE) +
  geom_linerange(mapping = aes(ymin = prop - ifelse(grepl("Old", condition), prop_old_low_se, ifelse(grepl("Similar", condition), prop_sim_low_se, prop_new_low_se)),
                                  ymax = prop + ifelse(grepl("Old", condition), prop_old_low_se, ifelse(grepl("Similar", condition), prop_sim_low_se, prop_new_low_se)),
                                  color = condition),
                    show.legend = FALSE) +
  ylim(0, 1) +
  ggtitle("Proportions of Old, Similar, and New Responses - MST_4T") +
  xlab("Similarity level") +
  ylab("Proportion") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("New - High Grip" = "#BC3C29FF", "New - Low Grip" = "#ea9999", "Old - High Grip" = "#0072B5FF", "Old - Low Grip" = "#CCE6FF", "Similar - High Grip" = "#20854EFF", "Similar - Low Grip" = "#a7eac6")) +
  scale_linetype_manual(values = c("New - High Grip" = "solid", "New - Low Grip" = "solid", "Old - High Grip" = "solid", "Old - Low Grip" = "solid", "Similar - High Grip" = "solid", "Similar - Low Grip" = "solid")) +
  guides(color = guide_legend(title = "Response Type", ncol = 1), linetype = "none")

6.8.3 Reaction Time

6.8.3.1 Using Median with within-subject SEM

# calculate subject mean
MST_4T_RT_ws <- MST_4T %>%
  group_by(sub, stimlev_mst, resp1) %>%
  mutate(sub_mean = mean(resp1RT, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
MST_4T_RT_ws <- MST_4T_RT_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

MST_4T_RT_ws <- MST_4T_RT_ws %>%
  mutate(RT_diff = sub_mean- grand_mean) 


# Calculate mean RT for old responses
    Old_medRT_MST_4T <- MST_4T_RT_ws %>%
        filter(resp1 == 'old') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(RT= median(resp1RT),RT_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(RT_old = median(RT),
                  RT_old_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
        distinct()
    
# Calculate mean RT for sim responses
    Sim_medRT_MST_4T <- MST_4T_RT_ws %>%
        filter(resp1 == 'sim') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(RT= median(resp1RT),RT_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(RT_sim = median(RT),
                  RT_sim_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
        distinct()
 
# Calculate mean RT for old responses
    New_medRT_MST_4T <- MST_4T_RT_ws %>%
        filter(resp1 == 'new') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(RT= median(resp1RT), RT_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(RT_new = median(RT),
                  RT_new_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
        distinct()
    
# Combine both datasets for plotting
    combined_medRT_MST_4T <- bind_rows(
        mutate(Old_medRT_MST_4T, condition = "Old"),
        mutate(Sim_medRT_MST_4T, condition = "Similar"),
        mutate(New_medRT_MST_4T, condition = "New")
    )
    
# Specify the desired order of levels for 'stimlev_mst'
    desired_order <- c("0targ", "1lure", "2lure", "3lure", "4lure", "5foil")
    
 # Convert 'stimlev_mst' to factor with the desired order
combined_medRT_MST_4T$stimlev_mst <- factor(combined_medRT_MST_4T$stimlev_mst, levels = desired_order)

#Sort the combined dataset by stimlev_mst within each condition
    combined_medRT_MST_4T <- combined_medRT_MST_4T %>%
        arrange(condition, stimlev_mst)
    
# Plotting
ggplot(combined_medRT_MST_4T, aes(x = stimlev_mst)) +
  geom_line(aes(y = RT_old, group = condition, color = "Old"), size = 1.2) +
  geom_line(aes(y = RT_sim, group = condition, color = "Similar"), size = 1.2) +
  geom_line(aes(y = RT_new, group = condition, color = "New"), size = 1.2) +
  geom_point(aes(y = RT_old, color = "Old"), size = 2) +
  geom_point(aes(y = RT_sim, color = "Similar"), size = 2) +
  geom_point(aes(y = RT_new, color = "New"), size = 2) +
  geom_linerange(aes(ymin = RT_old - RT_old_se, ymax = RT_old + RT_old_se), color = "#0072B5FF", size = 0.5) +
  geom_linerange(aes(ymin = RT_sim - RT_sim_se, ymax = RT_sim + RT_sim_se), color = "#20854EFF", size = 0.5) +
  geom_linerange(aes(ymin = RT_new - RT_new_se, ymax = RT_new + RT_new_se), color = "#BC3C29FF", size = 0.5) +
  ggtitle("Median of Median Reaction Time for Old, Similar, and New Responses - MST") +
  xlab("Stimulus Level") +
  ylab("Median Reaction Time") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values=c("Old"="#0072B5FF", "New"="#BC3C29FF", "Similar"="#20854EFF")) +
  guides(color = guide_legend(title = "Response Type"))

6.8.3.2 Using Median within and Mean between subjects with WSSEM

# calculate subject mean
MST_4T_RT_ws <- MST_4T %>%
  group_by(sub, stimlev_mst, resp1) %>%
  mutate(sub_mean = mean(resp1RT, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
MST_4T_RT_ws <- MST_4T_RT_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

MST_4T_RT_ws <- MST_4T_RT_ws %>%
  mutate(RT_diff = sub_mean- grand_mean) 


# Calculate mean RT for old responses
    Old_medRT_MST_4T <- MST_4T_RT_ws %>%
        filter(resp1 == 'old') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(RT= median(resp1RT),RT_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(RT_old = mean(RT, na.rm = T),
                  RT_old_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
        distinct()
    
# Calculate mean RT for sim responses
    Sim_medRT_MST_4T <- MST_4T_RT_ws %>%
        filter(resp1 == 'sim') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(RT= median(resp1RT),RT_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(RT_sim = mean(RT, na.rm = T),
                  RT_sim_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
        distinct()
 
# Calculate mean RT for old responses
    New_medRT_MST_4T <- MST_4T_RT_ws %>%
        filter(resp1 == 'new') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(RT= median(resp1RT), RT_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(RT_new = mean(RT, na.rm = T),
                  RT_new_se = sd(RT_diff, na.rm=T) /sqrt(n_distinct(sub))) %>%
        distinct()
    
# Combine both datasets for plotting
    combined_RT_MST_4T <- bind_rows(
        mutate(Old_medRT_MST_4T, condition = "Old"),
        mutate(Sim_medRT_MST_4T, condition = "Similar"),
        mutate(New_medRT_MST_4T, condition = "New")
    )
    
# Specify the desired order of levels for 'stimlev_mst'
    desired_order <- c("0targ", "1lure", "2lure", "3lure", "4lure", "5foil")
    
 # Convert 'stimlev_mst' to factor with the desired order
combined_RT_MST_4T$stimlev_mst <- factor(combined_RT_MST_4T$stimlev_mst, levels = desired_order)

#Sort the combined dataset by stimlev_mst within each condition
    combined_RT_MST_4T <- combined_RT_MST_4T %>%
        arrange(condition, stimlev_mst)
    
# Plotting
ggplot(combined_RT_MST_4T, aes(x = stimlev_mst)) +
  geom_line(aes(y = RT_old, group = condition, color = "Old"), size = 1.2) +
  geom_line(aes(y = RT_sim, group = condition, color = "Similar"), size = 1.2) +
  geom_line(aes(y = RT_new, group = condition, color = "New"), size = 1.2) +
  geom_point(aes(y = RT_old, color = "Old"), size = 2) +
  geom_point(aes(y = RT_sim, color = "Similar"), size = 2) +
  geom_point(aes(y = RT_new, color = "New"), size = 2) +
  geom_linerange(aes(ymin = RT_old - RT_old_se, ymax = RT_old + RT_old_se), color = "#0072B5FF", size = 0.5) +
  geom_linerange(aes(ymin = RT_sim - RT_sim_se, ymax = RT_sim + RT_sim_se), color = "#20854EFF", size = 0.5) +
  geom_linerange(aes(ymin = RT_new - RT_new_se, ymax = RT_new + RT_new_se), color = "#BC3C29FF", size = 0.5) +
  ggtitle("Median of Within and Mean Between Reaction Time - MST") +
  xlab("Stimulus Level") +
  ylab("Median Reaction Time") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values=c("Old"="#0072B5FF", "New"="#BC3C29FF", "Similar"="#20854EFF")) +
  guides(color = guide_legend(title = "Response Type"))

6.8.3.3 Handgrip manipulation with Median

# calculate subject mean
MST_4T_RT_ws <- MST_4T %>%
  group_by(sub, stimlev_mst, resp1) %>%
  mutate(sub_mean = mean(resp1RT, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
MST_4T_RT_ws <- MST_4T_RT_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

MST_4T_RT_ws <- MST_4T_RT_ws %>%
  mutate(RT_diff = sub_mean- grand_mean) 

# Calculate median RT for high - old responses
    Old_RT_high_grip_MST_4T <- MST_4T_RT_ws %>%
      filter(isStrength==1) %>%
      filter(resp1=='old') %>%
      group_by(stimlev_mst, sub) %>%
      summarise(RT= median(resp1RT),RT_diff) %>%
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(RT_old_high = median(RT),
                RT_old_high_se = sd(RT_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()

# Calculate median RT for low - old responses
    Old_RT_low_grip_MST_4T <- MST_4T_RT_ws %>%
      filter(isStrength==0) %>%
      filter(resp1=='old') %>%
      group_by(stimlev_mst, sub) %>%
      summarise(RT= median(resp1RT),RT_diff) %>%
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(RT_old_low = median(RT),
                RT_old_low_se = sd(RT_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()    
    
# Calculate median RT for high-sim responses
    Sim_RT_high_grip_MST_4T <- MST_4T_RT_ws %>%
      filter(isStrength==1) %>%
      filter(resp1=='sim') %>%
      group_by(stimlev_mst, sub) %>%
      summarise(RT= median(resp1RT),RT_diff) %>%
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(RT_sim_high = median(RT),
                RT_sim_high_se = sd(RT_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
    
# Calculate median RT  for low-sim responses
    Sim_RT_low_grip_MST_4T <- MST_4T_RT_ws %>%
      filter(isStrength==0) %>%
      filter(resp1=='sim') %>%
      group_by(stimlev_mst, sub) %>%
      summarise(RT= median(resp1RT),RT_diff) %>%
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(RT_sim_low = median(RT),
                RT_sim_low_se = sd(RT_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
        
# Calculate median RT  for high-new responses
    New_RT_high_grip_MST_4T <- MST_4T_RT_ws %>%
      filter(resp1=='new') %>%
      filter(isStrength==1) %>%
      group_by(stimlev_mst, sub) %>%
      summarise(RT= median(resp1RT),RT_diff) %>%
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(RT_new_high = median(RT),
                RT_new_high_se = sd(RT_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
   
# Calculate median RT for low-new responses
    New_RT_low_grip_MST_4T <- MST_4T_RT_ws %>%
      filter(resp1=='new') %>%
      filter(isStrength==0) %>%
      group_by(stimlev_mst, sub) %>%
      summarise(RT= median(resp1RT),RT_diff) %>%
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(RT_new_low = median(RT),
                RT_new_low_se = sd(RT_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
    
    
# Combine both datasets for plotting
combined_medRT_grip_MST_4T <- bind_rows(
  mutate(Old_RT_high_grip_MST_4T, condition = "Old - High Grip", RT = RT_old_high),
  mutate(Old_RT_low_grip_MST_4T, condition = "Old - Low Grip", RT = RT_old_low),
  mutate(Sim_RT_high_grip_MST_4T, condition = "Similar - High Grip", RT = RT_sim_high),
  mutate(Sim_RT_low_grip_MST_4T, condition = "Similar - Low Grip", RT = RT_sim_low),
  mutate(New_RT_high_grip_MST_4T, condition = "New - High Grip", RT = RT_new_high),
  mutate(New_RT_low_grip_MST_4T, condition = "New - Low Grip", RT = RT_new_low)
)

  combined_medRT_grip_MST_4T$stimlev_mst <- factor(combined_medRT_grip_MST_4T$stimlev_mst, levels = desired_order)

# Specify the desired order of levels for 'stimlev_mst'
    desired_order <- c("0targ", "1lure", "2lure", "3lure", "4lure", "5foil")

# Convert 'stimlev_mst' to factor with the desired order
  combined_medRT_grip_MST_4T$stimlev_mst <- factor(combined_medRT_grip_MST_4T$stimlev_mst, levels = desired_order)

# Plotting without jitter
ggplot(data = combined_medRT_grip_MST_4T) +
  geom_point(aes(x = stimlev_mst, y = RT_old_high, color = condition), size = 2) +
  geom_line(aes(x = stimlev_mst, y = RT_old_high, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimlev_mst, ymin = RT_old_high - RT_old_high_se, 
                     ymax = RT_old_high + RT_old_high_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimlev_mst, y = RT_old_low, color = condition), size = 2) +
  geom_line(aes(x = stimlev_mst, y = RT_old_low, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimlev_mst, ymin = RT_old_low - RT_old_low_se, 
                     ymax = RT_old_low + RT_old_low_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimlev_mst, y = RT_sim_high, color = condition), size = 2) +
  geom_line(aes(x = stimlev_mst, y = RT_sim_high, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimlev_mst, ymin = RT_sim_high - RT_sim_high_se, 
                     ymax = RT_sim_high + RT_sim_high_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimlev_mst, y = RT_sim_low, color = condition), size = 2) +
  geom_line(aes(x = stimlev_mst, y = RT_sim_low, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimlev_mst, ymin = RT_sim_low - RT_sim_low_se, 
                     ymax = RT_sim_low + RT_sim_low_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimlev_mst, y = RT_new_high, color = condition), size = 2) +
  geom_line(aes(x = stimlev_mst, y = RT_new_high, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimlev_mst, ymin = RT_new_high - RT_new_high_se, 
                     ymax = RT_new_high + RT_new_high_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimlev_mst, y = RT_new_low, color = condition), size = 2) +
  geom_line(aes(x = stimlev_mst, y = RT_new_low, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimlev_mst, ymin = RT_new_low - RT_new_low_se, 
                     ymax = RT_new_low + RT_new_low_se, color = condition), 
                 linetype = "solid") +
  
  
  ggtitle("Median of Reaction Time for Same and Different Responses - MST") +
  xlab("Similarity level") +
  ylab("Reaction Time") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("New - High Grip" = "#BC3C29FF", "New - Low Grip" = "#ea9999", "Old - High Grip" = "#0072B5FF", "Old - Low Grip" = "#CCE6FF", "Similar - High Grip" = "#20854EFF", "Similar - Low Grip" = "#a7eac6")) +
  scale_linetype_manual(values = c("New - High Grip" = "solid", "New - Low Grip" = "solid", "Old - High Grip" = "solid", "Old - Low Grip" = "solid", "Similar - High Grip" = "solid", "Similar - Low Grip" = "solid")) +
  guides(color = guide_legend(title = "Response Type", ncol = 1), linetype = "none")

6.8.3.4 Handgrip manipulation with Median within and Mean between with WSSEM

# calculate subject mean
MST_4T_RT_ws <- MST_4T %>%
  group_by(sub, stimlev_mst, resp1) %>%
  mutate(sub_mean = mean(resp1RT, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
MST_4T_RT_ws <- MST_4T_RT_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

MST_4T_RT_ws <- MST_4T_RT_ws %>%
  mutate(RT_diff = sub_mean- grand_mean) 

# Calculate median RT for high - old responses
    Old_RT_high_grip_MST_4T <- MST_4T_RT_ws %>%
      filter(isStrength==1) %>%
      filter(resp1=='old') %>%
      group_by(stimlev_mst, sub) %>%
      summarise(RT= median(resp1RT),RT_diff) %>%
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(RT_old_high = mean(RT, na.rm = T),
                RT_old_high_se = sd(RT_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()

# Calculate median RT for low - old responses
    Old_RT_low_grip_MST_4T <- MST_4T_RT_ws %>%
      filter(isStrength==0) %>%
      filter(resp1=='old') %>%
      group_by(stimlev_mst, sub) %>%
      summarise(RT= median(resp1RT),RT_diff) %>%
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(RT_old_low = mean(RT, na.rm = T),
                RT_old_low_se = sd(RT_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()    
    
# Calculate median RT for high-sim responses
    Sim_RT_high_grip_MST_4T <- MST_4T_RT_ws %>%
      filter(isStrength==1) %>%
      filter(resp1=='sim') %>%
      group_by(stimlev_mst, sub) %>%
      summarise(RT= median(resp1RT),RT_diff) %>%
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(RT_sim_high = mean(RT, na.rm = T),
                RT_sim_high_se = sd(RT_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
    
# Calculate median RT  for low-sim responses
    Sim_RT_low_grip_MST_4T <- MST_4T_RT_ws %>%
      filter(isStrength==0) %>%
      filter(resp1=='sim') %>%
      group_by(stimlev_mst, sub) %>%
      summarise(RT= median(resp1RT),RT_diff) %>%
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(RT_sim_low = mean(RT, na.rm = T),
                RT_sim_low_se = sd(RT_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
        
# Calculate median RT  for high-new responses
    New_RT_high_grip_MST_4T <- MST_4T_RT_ws %>%
      filter(resp1=='new') %>%
      filter(isStrength==1) %>%
      group_by(stimlev_mst, sub) %>%
      summarise(RT= median(resp1RT),RT_diff) %>%
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(RT_new_high = mean(RT, na.rm = T),
                RT_new_high_se = sd(RT_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
   
# Calculate median RT for low-new responses
    New_RT_low_grip_MST_4T <- MST_4T_RT_ws %>%
      filter(resp1=='new') %>%
      filter(isStrength==0) %>%
      group_by(stimlev_mst, sub) %>%
      summarise(RT= median(resp1RT),RT_diff) %>%
      ungroup() %>%
      group_by(stimlev_mst) %>%
      summarise(RT_new_low = mean(RT, na.rm = T),
                RT_new_low_se = sd(RT_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
      distinct()
    
    
# Combine both datasets for plotting
combined_RT_grip_MST_4T <- bind_rows(
  mutate(Old_RT_high_grip_MST_4T, condition = "Old - High Grip", RT = RT_old_high),
  mutate(Old_RT_low_grip_MST_4T, condition = "Old - Low Grip", RT = RT_old_low),
  mutate(Sim_RT_high_grip_MST_4T, condition = "Similar - High Grip", RT = RT_sim_high),
  mutate(Sim_RT_low_grip_MST_4T, condition = "Similar - Low Grip", RT = RT_sim_low),
  mutate(New_RT_high_grip_MST_4T, condition = "New - High Grip", RT = RT_new_high),
  mutate(New_RT_low_grip_MST_4T, condition = "New - Low Grip", RT = RT_new_low)
)

  combined_RT_grip_MST_4T$stimlev_mst <- factor(combined_RT_grip_MST_4T$stimlev_mst, levels = desired_order)

# Specify the desired order of levels for 'stimlev_mst'
    desired_order <- c("0targ", "1lure", "2lure", "3lure", "4lure", "5foil")

# Convert 'stimlev_mst' to factor with the desired order
  combined_RT_grip_MST_4T$stimlev_mst <- factor(combined_RT_grip_MST_4T$stimlev_mst, levels = desired_order)

# Plotting without jitter
ggplot(data = combined_RT_grip_MST_4T) +
  geom_point(aes(x = stimlev_mst, y = RT_old_high, color = condition), size = 2) +
  geom_line(aes(x = stimlev_mst, y = RT_old_high, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimlev_mst, ymin = RT_old_high - RT_old_high_se, 
                     ymax = RT_old_high + RT_old_high_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimlev_mst, y = RT_old_low, color = condition), size = 2) +
  geom_line(aes(x = stimlev_mst, y = RT_old_low, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimlev_mst, ymin = RT_old_low - RT_old_low_se, 
                     ymax = RT_old_low + RT_old_low_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimlev_mst, y = RT_sim_high, color = condition), size = 2) +
  geom_line(aes(x = stimlev_mst, y = RT_sim_high, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimlev_mst, ymin = RT_sim_high - RT_sim_high_se, 
                     ymax = RT_sim_high + RT_sim_high_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimlev_mst, y = RT_sim_low, color = condition), size = 2) +
  geom_line(aes(x = stimlev_mst, y = RT_sim_low, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimlev_mst, ymin = RT_sim_low - RT_sim_low_se, 
                     ymax = RT_sim_low + RT_sim_low_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimlev_mst, y = RT_new_high, color = condition), size = 2) +
  geom_line(aes(x = stimlev_mst, y = RT_new_high, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimlev_mst, ymin = RT_new_high - RT_new_high_se, 
                     ymax = RT_new_high + RT_new_high_se, color = condition), 
                 linetype = "solid") +
  
  geom_point(aes(x = stimlev_mst, y = RT_new_low, color = condition), size = 2) +
  geom_line(aes(x = stimlev_mst, y = RT_new_low, color = condition, group = condition), size = 1.2) +
  geom_linerange(aes(x = stimlev_mst, ymin = RT_new_low - RT_new_low_se, 
                     ymax = RT_new_low + RT_new_low_se, color = condition), 
                 linetype = "solid") +
  
  
  ggtitle("Median of Reaction Time Within and Mean Between - MST") +
  xlab("Similarity level") +
  ylab("Reaction Time") +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("New - High Grip" = "#BC3C29FF", "New - Low Grip" = "#ea9999", "Old - High Grip" = "#0072B5FF", "Old - Low Grip" = "#CCE6FF", "Similar - High Grip" = "#20854EFF", "Similar - Low Grip" = "#a7eac6")) +
  scale_linetype_manual(values = c("New - High Grip" = "solid", "New - Low Grip" = "solid", "Old - High Grip" = "solid", "Old - Low Grip" = "solid", "Similar - High Grip" = "solid", "Similar - Low Grip" = "solid")) +
  guides(color = guide_legend(title = "Response Type", ncol = 1), linetype = "none")

6.8.4 Confidence

# calculate subject mean
MST_4T_conf_ws <- MST_4T %>%
  group_by(sub, stimlev_mst, resp1_diff) %>%
  mutate(sub_mean = mean(resp2, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
MST_4T_conf_ws <- MST_4T_conf_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

MST_4T_conf_ws <- MST_4T_conf_ws %>%
  mutate(conf_diff = sub_mean- grand_mean)

# Calculate mean confidence for old responses
    Old_conf_MST_4T <- MST_4T_conf_ws %>%
        filter(resp1 == 'old') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(conf_prop= mean(resp2, na.rm = T),conf_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(conf_old = mean(conf_prop, na.rm = T),
                  conf_old_se = sd(conf_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
        distinct()
# Calculate mean confidence for sim responses
    Sim_conf_MST_4T <- MST_4T_conf_ws %>%
        filter(resp1 == 'sim') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(conf_prop= mean(resp2, na.rm = T),conf_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(conf_sim = mean(conf_prop, na.rm = T),
                  conf_sim_se = sd(conf_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
        distinct()
# Calculate mean confidence for new responses
    New_conf_MST_4T <- MST_4T_conf_ws %>%
        filter(resp1 == 'new') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(conf_prop= mean(resp2, na.rm = T),conf_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(conf_new = mean(conf_prop, na.rm = T),
                  conf_new_se = sd(conf_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
        distinct()
    
# Combine both datasets for plotting
    combined_conf_MST_4T <- bind_rows(
        mutate(Old_conf_MST_4T, condition = "Old"),
        mutate(Sim_conf_MST_4T, condition = "Similar"),
        mutate(New_conf_MST_4T, condition = "New")
    )
    
    # Specify the desired order of levels for 'stimlev_mst'
    desired_order <- c("0targ", "1lure", "2lure", "3lure", "4lure", "5foil")
    
    # Convert 'stimlev_mst' to factor with the desired order
    combined_conf_MST_4T$stimlev_mst <- factor(combined_conf_MST_4T$stimlev_mst, levels = desired_order)
    
    # Sort the combined dataset by stimlev_mst within each condition
    combined_conf_MST_4T <- combined_conf_MST_4T %>%
        arrange(condition, stimlev_mst)
    
    # Plotting
    ggplot(combined_conf_MST_4T, aes(x = stimlev_mst)) +
  geom_line(aes(y = conf_old, group = condition, color = "Old"), size = 1.2) +
  geom_line(aes(y = conf_sim, group = condition, color = "Similar"), size = 1.2) +
  geom_line(aes(y = conf_new, group = condition, color = "New"), size = 1.2) +
  geom_point(aes(y = conf_old, color = "Old"), size = 2) +
  geom_point(aes(y = conf_sim, color = "Similar"), size = 2) +
  geom_point(aes(y = conf_new, color = "New"), size = 2) +
  geom_linerange(aes(ymin = conf_old - conf_old_se, ymax = conf_old + conf_old_se), color = "#0072B5FF", size = 0.5) +
  geom_linerange(aes(ymin = conf_sim - conf_sim_se, ymax = conf_sim + conf_sim_se), color = "#20854EFF", size = 0.5) +
  geom_linerange(aes(ymin = conf_new - conf_new_se, ymax = conf_new + conf_new_se), color = "#BC3C29FF", size = 0.5) +
  ggtitle("Average of Confidence for Old, Similar, and New Responses - MST") +
  xlab("Stimulus Level") +
  ylab("Confidence Rating") +
  ylim(2,4)+
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values=c("Old"="#0072B5FF", "New"="#BC3C29FF", "Similar"="#20854EFF")) +
  guides(color = guide_legend(title = "Response Type"))

6.8.4.1 Handgrip manipulation

# calculate subject mean
MST_4T_conf_ws <- MST_4T %>%
  group_by(sub, stimlev_mst, resp1_diff) %>%
  mutate(sub_mean = mean(resp2, na.rm=T)) %>%
  distinct() %>%
  ungroup()

# calculate the grand mean
MST_4T_conf_ws <- MST_4T_conf_ws %>%
  group_by(sub) %>%
  mutate(grand_mean = mean(sub_mean)) %>%
  ungroup()

# subtract the subject mean and add grand mean for each cell

MST_4T_conf_ws <- MST_4T_conf_ws %>%
  mutate(conf_diff = sub_mean- grand_mean)

# Calculate mean confidence for High old responses
    Old_conf_high_grip_MST_4T <- MST_4T_conf_ws %>%
        filter(isStrength==1) %>%
        filter(resp1 == 'old') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(conf_prop= mean(resp2, na.rm = T),conf_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(conf_high_old = mean(conf_prop, na.rm = T),
                  conf_high_old_se = sd(conf_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
        distinct()
    
# Calculate mean confidence for low old responses
    Old_conf_low_grip_MST_4T <- MST_4T_conf_ws %>%
        filter(isStrength==0) %>%
        filter(resp1 == 'old') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(conf_prop= mean(resp2, na.rm = T),conf_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(conf_low_old = mean(conf_prop, na.rm = T),
                  conf_low_old_se = sd(conf_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
        distinct()
    
# Calculate mean confidence for High sim responses
    Sim_conf_high_grip_MST_4T <- MST_4T_conf_ws %>%
        filter(isStrength==1) %>%
        filter(resp1 == 'sim') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(conf_prop= mean(resp2, na.rm = T),conf_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(conf_high_sim = mean(conf_prop, na.rm = T),
                  conf_high_sim_se = sd(conf_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
        distinct()
    
# Calculate mean confidence for Low sim responses
    Sim_conf_low_grip_MST_4T <- MST_4T_conf_ws %>%
        filter(isStrength==0) %>%
        filter(resp1 == 'sim') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(conf_prop= mean(resp2, na.rm = T),conf_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(conf_low_sim = mean(conf_prop, na.rm = T),
                  conf_low_sim_se = sd(conf_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
        distinct()
    
# Calculate mean confidence for High new responses
    New_conf_high_grip_MST_4T <- MST_4T_conf_ws %>%
        filter(isStrength ==1) %>%
        filter(resp1 == 'new') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(conf_prop= mean(resp2, na.rm = T),conf_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(conf_high_new = mean(conf_prop, na.rm = T),
                  conf_high_new_se = sd(conf_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
        distinct()
# Calculate mean confidence for Low new responses
    New_conf_low_grip_MST_4T <- MST_4T_conf_ws %>%
        filter(isStrength ==0) %>%
        filter(resp1 == 'new') %>%
        group_by(stimlev_mst,sub) %>%
        summarise(conf_prop= mean(resp2, na.rm = T),conf_diff) %>%
        ungroup() %>%
        group_by(stimlev_mst) %>%
        summarise(conf_low_new = mean(conf_prop, na.rm = T),
                  conf_low_new_se = sd(conf_diff, na.rm = T) /sqrt(n_distinct(sub))) %>%
        distinct()    
    
# Combine both datasets for plotting
    combined_conf_grip_MST_4T <- bind_rows(
        mutate(Old_conf_high_grip_MST_4T, condition = "Old - High Grip"),
        mutate(Sim_conf_high_grip_MST_4T, condition = "Similar - High Grip"),
        mutate(New_conf_high_grip_MST_4T, condition = "New - High Grip"),
        mutate(Old_conf_low_grip_MST_4T, condition = "Old - Low Grip"),
        mutate(Sim_conf_low_grip_MST_4T, condition = "Similar - Low Grip"),
        mutate(New_conf_low_grip_MST_4T, condition = "New - Low Grip")
    )

    
    # Specify the desired order of levels for 'stimlev_mst'
    desired_order <- c("0targ", "1lure", "2lure", "3lure", "4lure", "5foil")
    
    # Convert 'stimlev_mst' to factor with the desired order
    combined_conf_grip_MST_4T$stimlev_mst <- factor(combined_conf_grip_MST_4T$stimlev_mst, levels = desired_order)
    
    # Sort the combined dataset by stimlev_mst within each condition
    combined_conf_grip_MST_4T <- combined_conf_grip_MST_4T %>%
        arrange(condition, stimlev_mst)
    
ggplot(combined_conf_grip_MST_4T, aes(x = stimlev_mst)) +
  geom_line(aes(y = conf_high_old, group = condition, color = condition), size = 1.2) +
  geom_point(aes(y = conf_high_old, color = condition), size = 2) +
  geom_linerange(aes(ymin = conf_high_old - conf_high_old_se, ymax = conf_high_old + conf_high_old_se, color = condition), size = 0.5) +
  geom_line(aes(y = conf_high_sim, group = condition, color = condition), size = 1.2, linetype = "solid") +
  geom_point(aes(y = conf_high_sim, color = condition), size = 2) +
  geom_linerange(aes(ymin = conf_high_sim - conf_high_sim_se, ymax = conf_high_sim + conf_high_sim_se, color = condition), size = 0.5) +
  geom_line(aes(y = conf_high_new, group = condition, color = condition), size = 1.2, linetype = "solid") +
  geom_point(aes(y = conf_high_new, color = condition), size = 2) +
  geom_linerange(aes(ymin = conf_high_new - conf_high_new_se, ymax = conf_high_new + conf_high_new_se, color = condition), size = 0.5) +
  geom_line(aes(y = conf_low_old, group = condition, color = condition), size = 1.2) +
  geom_point(aes(y = conf_low_old, color = condition), size = 2) +
  geom_linerange(aes(ymin = conf_low_old - conf_low_old_se, ymax = conf_low_old + conf_low_old_se, color = condition), size = 0.5) +
  geom_line(aes(y = conf_low_sim, group = condition, color = condition), size = 1.2, linetype = "solid") +
  geom_point(aes(y = conf_low_sim, color = condition), size = 2) +
  geom_linerange(aes(ymin = conf_low_sim - conf_low_sim_se, ymax = conf_low_sim + conf_low_sim_se, color = condition), size = 0.5) +
  geom_line(aes(y = conf_low_new, group = condition, color = condition), size = 1.2, linetype = "solid") +
  geom_point(aes(y = conf_low_new, color = condition), size = 2) +
  geom_linerange(aes(ymin = conf_low_new - conf_low_new_se, ymax = conf_low_new + conf_low_new_se, color = condition), size = 0.5) +
  ggtitle("Average of Confidence for Old, Similar, and New Responses - MST") +
  xlab("Stimulus Level") +
  ylab("Confidence Rating") +
  ylim(2, 4) +
  labs(caption = "Error bars represent within-subject SEM") +
  scale_color_manual(values = c("#0072B5FF", "#CCE6FF", "#20854EFF", "#a7eac6", "#BC3C29FF", "#ea9999"),
                     name = "Condition",
                     labels = c("Old - High Grip", "Old - Low Grip", "Similar - High Grip", "Similar - Low Grip", "New - High Grip", "New - Low Grip")) +
  scale_linetype_manual(values = c("solid", "solid", "solid", "solid", "solid", "solid"), 
                        name = "Condition",
                        labels = c("Old - High Grip", "Old - Low Grip", "Similar - High Grip", "Similar - Low Grip", "New - High Grip", "New - Low Grip")) +
  guides(color = guide_legend(title = "Response Type"))

6.9 Statistical Analysis

6.9.1 Effect of Grip on responses

6.9.1.1 The Effect of Grip on ‘Old’ Responses across Stimulus Levels

MST_anova_grip_old <- MST_4T %>%
  filter(stimlev_mst!= '5foil') %>%  group_by(stimlev_mst, isStrength, sub) %>%
  mutate(resp1_old = case_when(resp1 == 'old' ~ 1, TRUE ~ 0)) %>%  
  ungroup() %>% group_by(stimlev_mst, isStrength) %>% summarise(old = mean(resp1_old, na.rm = T), stimlev_mst, isStrength) %>%
  distinct()

aov(old ~ (stimlev_mst * isStrength), MST_anova_grip_old) 
## Call:
##    aov(formula = old ~ (stimlev_mst * isStrength), data = MST_anova_grip_old)
## 
## Terms:
##                 stimlev_mst isStrength stimlev_mst:isStrength
## Sum of Squares    0.4460692  0.0014966              0.0007266
## Deg. of Freedom           4          1                      4
## 
## Estimated effects may be unbalanced

6.9.1.2 The Effect of Grip on ‘Similar’ Responses across Stimulus Levels

MST_anova_grip_sim <- MST_4T %>%
  filter(stimlev_mst!= '5foil') %>%  group_by(stimlev_mst, isStrength, sub) %>%
  mutate(resp1_sim = case_when(resp1 == 'sim' ~ 1, TRUE ~ 0)) %>%  
  ungroup() %>% group_by(stimlev_mst, isStrength) %>% summarise(sim = mean(resp1_sim, na.rm = T), stimlev_mst, isStrength) %>%
  distinct()

aov(sim ~ (stimlev_mst * isStrength), MST_anova_grip_sim) 
## Call:
##    aov(formula = sim ~ (stimlev_mst * isStrength), data = MST_anova_grip_sim)
## 
## Terms:
##                 stimlev_mst isStrength stimlev_mst:isStrength
## Sum of Squares    0.3164611  0.0006756              0.0006532
## Deg. of Freedom           4          1                      4
## 
## Estimated effects may be unbalanced

6.9.1.3 The Effect of Grip on ‘New’ Responses across Stimulus Levels

MST_anova_grip_new <- MST_4T %>%
  filter(stimlev_mst!= '5foil') %>%  group_by(stimlev_mst, isStrength, sub) %>%
  mutate(resp1_new = case_when(resp1 == 'new' ~ 1, TRUE ~ 0)) %>%  
  ungroup() %>% group_by(stimlev_mst, isStrength) %>% summarise(new = mean(resp1_new, na.rm = T), stimlev_mst, isStrength) %>%
  distinct()

aov(new ~ (stimlev_mst * isStrength), MST_anova_grip_new) 
## Call:
##    aov(formula = new ~ (stimlev_mst * isStrength), data = MST_anova_grip_new)
## 
## Terms:
##                 stimlev_mst  isStrength stimlev_mst:isStrength
## Sum of Squares  0.011405018 0.000161116            0.000193271
## Deg. of Freedom           4           1                      4
## 
## Estimated effects may be unbalanced

6.9.2 Effect of Grip on Reaction Time

6.9.2.1 The Effect of Grip on Median Reaction Time for ‘Old’ responses

MST_anova_grip_RT_old <- MST_4T %>%
  filter(stimlev_mst!= '5foil') %>% filter(resp1 =='old') %>% group_by(stimlev_mst, isStrength, sub) %>% mutate(RT_sub = median(resp1RT)) %>%
  ungroup() %>% group_by(stimlev_mst, isStrength) %>% summarise(RT = median(RT_sub), stimlev_mst, isStrength) %>%
  distinct()
  

aov(RT ~ (stimlev_mst * isStrength), MST_anova_grip_RT_old) 
## Call:
##    aov(formula = RT ~ (stimlev_mst * isStrength), data = MST_anova_grip_RT_old)
## 
## Terms:
##                 stimlev_mst  isStrength stimlev_mst:isStrength
## Sum of Squares  0.027665940 0.005536493            0.020273193
## Deg. of Freedom           4           1                      4
## 
## Estimated effects may be unbalanced

6.9.2.2 The Effect of Grip on Median Reaction Time for ‘Similar’ responses

MST_anova_grip_RT_sim <- MST_4T %>%
  filter(stimlev_mst!= '5foil') %>% filter(resp1 =='sim') %>% group_by(stimlev_mst, isStrength, sub) %>% mutate(RT_sub = median(resp1RT)) %>%
  ungroup() %>% group_by(stimlev_mst, isStrength) %>% summarise(RT = median(RT_sub), stimlev_mst, isStrength) %>%
  distinct()
  

aov(RT ~ (stimlev_mst * isStrength), MST_anova_grip_RT_sim) 
## Call:
##    aov(formula = RT ~ (stimlev_mst * isStrength), data = MST_anova_grip_RT_sim)
## 
## Terms:
##                 stimlev_mst  isStrength stimlev_mst:isStrength
## Sum of Squares  0.020015423 0.004970802            0.001055577
## Deg. of Freedom           4           1                      4
## 
## Estimated effects may be unbalanced

6.9.2.3 The Effect of Grip on Median Reaction Time for ‘New’ responses

MST_anova_grip_RT_new <- MST_4T %>%
  filter(stimlev_mst!= '5foil') %>% filter(resp1 =='new') %>% group_by(stimlev_mst, isStrength, sub) %>% mutate(RT_sub = median(resp1RT)) %>%
  ungroup() %>% group_by(stimlev_mst, isStrength) %>% summarise(RT = median(RT_sub), stimlev_mst, isStrength) %>%
  distinct()
  

aov(RT ~ (stimlev_mst * isStrength), MST_anova_grip_RT_new) 
## Call:
##    aov(formula = RT ~ (stimlev_mst * isStrength), data = MST_anova_grip_RT_new)
## 
## Terms:
##                 stimlev_mst  isStrength stimlev_mst:isStrength
## Sum of Squares  0.013780635 0.001991678            0.015189757
## Deg. of Freedom           4           1                      4
## 
## Estimated effects may be unbalanced

6.9.3 Effect of Grip on Confidence Ratings

6.9.3.1 The Effect of Grip on Confidence Reports for ‘Old’ responses

MST_anova_grip_conf_diff <- MST_4T %>%
  filter(stimlev_mst!= '5foil') %>% filter(stimlev_mst!= '0targ') %>% filter(resp1 =='old') %>% group_by(stimlev_mst, isStrength, sub) %>% mutate(conf_sub = mean(resp2, na.rm = T)) %>%
  ungroup() %>% group_by(stimlev_mst, isStrength) %>% summarise(conf = mean(conf_sub, na.rm = T), stimlev_mst, isStrength) %>%
  distinct()
  

aov(conf ~ (stimlev_mst * isStrength), MST_anova_grip_conf_diff)
## Call:
##    aov(formula = conf ~ (stimlev_mst * isStrength), data = MST_anova_grip_conf_diff)
## 
## Terms:
##                 stimlev_mst isStrength stimlev_mst:isStrength
## Sum of Squares   0.11309127 0.00012877             0.01850553
## Deg. of Freedom           3          1                      3
## 
## Estimated effects may be unbalanced

6.9.3.2 The Effect of Grip on Confidence Reports for ‘Similar’ responses

MST_anova_grip_conf_same <- MST_4T %>%
  filter(stimlev_mst!= '5foil') %>% filter(stimlev_mst!= '0targ') %>% filter(resp1 =='sim') %>% group_by(stimlev_mst, isStrength, sub) %>% mutate(conf_sub = mean(resp2, na.rm = T)) %>%
  ungroup() %>% group_by(stimlev_mst, isStrength) %>% summarise(conf = median(conf_sub, na.rm=T), stimlev_mst, isStrength) %>%
  distinct()
  

aov(conf ~ (stimlev_mst * isStrength), MST_anova_grip_conf_same) 
## Call:
##    aov(formula = conf ~ (stimlev_mst * isStrength), data = MST_anova_grip_conf_same)
## 
## Terms:
##                 stimlev_mst  isStrength stimlev_mst:isStrength
## Sum of Squares  0.010416667 0.003472222            0.010416667
## Deg. of Freedom           3           1                      3
## 
## Estimated effects may be unbalanced

6.9.3.3 The Effect of Grip on Confidence Reports for ‘New’ responses

MST_anova_grip_conf_same <- MST_4T %>%
  filter(stimlev_mst!= '5foil') %>% filter(stimlev_mst!= '0targ') %>% filter(resp1 =='new') %>% group_by(stimlev_mst, isStrength, sub) %>% mutate(conf_sub = mean(resp2, na.rm = T)) %>%
  ungroup() %>% group_by(stimlev_mst, isStrength) %>% summarise(conf = median(conf_sub, na.rm=T), stimlev_mst, isStrength) %>%
  distinct()
  

aov(conf ~ (stimlev_mst * isStrength), MST_anova_grip_conf_same) 
## Call:
##    aov(formula = conf ~ (stimlev_mst * isStrength), data = MST_anova_grip_conf_same)
## 
## Terms:
##                 stimlev_mst isStrength stimlev_mst:isStrength
## Sum of Squares    0.8370833  0.0401389              0.3870833
## Deg. of Freedom           3          1                      3
## 
## Estimated effects may be unbalanced

6.10 Statistical analysis for all tasks

6.10.1 Effect of Grip on responding ‘Different’ across tasks and stimulus levels

MST_anova_grip_old_combined <- MST_4T %>% group_by(stimlev_mst, isStrength, sub) %>%
  mutate(resp1_old = case_when(resp1 == 'old' ~ 1, TRUE ~ 0)) %>%
  summarise(old = mean(resp1_old, na.rm = TRUE), stimlev_mst, isStrength, sub) %>%
  distinct()
MST_anova_grip_old_combined$stimlev_mst <- as.factor(MST_anova_grip_old_combined$stimlev_mst)
MST_anova_grip_old_combined$stimlev_mst <- recode_factor(MST_anova_grip_old_combined$stimlev_mst,'0targ'="0", '1lure'="1", '2lure'="2", '3lure'="3", '4lure'="4")
MST_anova_grip_old_combined <- MST_anova_grip_old_combined %>% filter(stimlev_mst!='5foil')
MST_anova_grip_old_combined <- rename(MST_anova_grip_old_combined, stimLev = stimlev_mst)
MST_anova_grip_old_combined <- MST_anova_grip_old_combined %>% mutate(old= 1-old)
MST_anova_grip_old_combined <- rename(MST_anova_grip_old_combined, diff = old)
MST_anova_grip_old_combined$task <- "MST"


ADT_anova_grip_diff_combined <- ADT_4T %>% group_by(stimLev, isStrength, sub) %>%
  summarise(diff = mean(resp1_diff, na.rm = TRUE), stimLev, isStrength, sub) %>%
  distinct()
ADT_anova_grip_diff_combined$stimLev <- as.factor(ADT_anova_grip_diff_combined$stimLev)
ADT_anova_grip_diff_combined$stimLev <- recode_factor(ADT_anova_grip_diff_combined$stimLev, '0'="0", '4'="1", '8'="2", '32'="3", '128'="4")
ADT_anova_grip_diff_combined$task <- "ADT"

VDT_anova_grip_diff_combined <- VDT_4T %>% group_by(stimLev, isStrength, sub) %>%
  summarise(diff = mean(resp1_diff, na.rm = TRUE), stimLev, isStrength, sub) %>%
  distinct()
VDT_anova_grip_diff_combined$stimLev <- as.factor(VDT_anova_grip_diff_combined$stimLev)
VDT_anova_grip_diff_combined$stimLev <- recode_factor(VDT_anova_grip_diff_combined$stimLev,'0'="0",'0.04'="1", '0.08'="2", '0.16'="3", '0.32'="4")
VDT_anova_grip_diff_combined$task <- "VDT"

CDT_anova_grip_diff_combined <- CDT_4T %>% group_by(stimLev, isStrength, sub) %>%
  summarise(diff = mean(resp1_diff, na.rm = TRUE), stimLev, isStrength, sub) %>%
  distinct()
CDT_anova_grip_diff_combined$stimLev <- as.factor(CDT_anova_grip_diff_combined$stimLev)
CDT_anova_grip_diff_combined$stimLev <- recode_factor(CDT_anova_grip_diff_combined$stimLev,'0'="0",'5'="1", '20'="2", '45'="3", '90'="4")
CDT_anova_grip_diff_combined$task <- "CDT"

alltask_anova_grip_diff <- rbind(ADT_anova_grip_diff_combined,VDT_anova_grip_diff_combined,CDT_anova_grip_diff_combined,MST_anova_grip_old_combined)
alltask_anova_grip_diff$isStrength <- as.factor(alltask_anova_grip_diff$isStrength)
alltask_anova_grip_diff$task <- as.factor(alltask_anova_grip_diff$task)


library(lme4)
model_ADT <- lmer(diff ~  isStrength + stimLev + (1 | sub), data = ADT_anova_grip_diff_combined)

summary(model_ADT)
## Linear mixed model fit by REML ['lmerMod']
## Formula: diff ~ isStrength + stimLev + (1 | sub)
##    Data: ADT_anova_grip_diff_combined
## 
## REML criterion at convergence: -486.9
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -4.8282 -0.3896 -0.0338  0.3667  5.3715 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  sub      (Intercept) 0.004459 0.06678 
##  Residual             0.012295 0.11088 
## Number of obs: 370, groups:  sub, 37
## 
## Fixed effects:
##              Estimate Std. Error t value
## (Intercept)  0.033679   0.017886   1.883
## isStrength  -0.008428   0.011529  -0.731
## stimLev1     0.014238   0.018229   0.781
## stimLev2     0.124516   0.018229   6.831
## stimLev3     0.818278   0.018229  44.889
## stimLev4     0.940724   0.018229  51.606
## 
## Correlation of Fixed Effects:
##            (Intr) isStrn stmLv1 stmLv2 stmLv3
## isStrength -0.322                            
## stimLev1   -0.510  0.000                     
## stimLev2   -0.510  0.000  0.500              
## stimLev3   -0.510  0.000  0.500  0.500       
## stimLev4   -0.510  0.000  0.500  0.500  0.500
report(model_ADT)
## We fitted a linear mixed model (estimated using REML and nloptwrap optimizer)
## to predict diff with isStrength and stimLev (formula: diff ~ isStrength +
## stimLev). The model included sub as random effect (formula: ~1 | sub). The
## model's total explanatory power is substantial (conditional R2 = 0.93) and the
## part related to the fixed effects alone (marginal R2) is of 0.91. The model's
## intercept, corresponding to isStrength = 0 and stimLev = 0, is at 0.03 (95% CI
## [-1.49e-03, 0.07], t(362) = 1.88, p = 0.060). Within this model:
## 
##   - The effect of isStrength is statistically non-significant and negative (beta
## = -8.43e-03, 95% CI [-0.03, 0.01], t(362) = -0.73, p = 0.465; Std. beta =
## -9.76e-03, 95% CI [-0.04, 0.02])
##   - The effect of stimLev [1] is statistically non-significant and positive (beta
## = 0.01, 95% CI [-0.02, 0.05], t(362) = 0.78, p = 0.435; Std. beta = 0.03, 95%
## CI [-0.05, 0.12])
##   - The effect of stimLev [2] is statistically significant and positive (beta =
## 0.12, 95% CI [0.09, 0.16], t(362) = 6.83, p < .001; Std. beta = 0.29, 95% CI
## [0.21, 0.37])
##   - The effect of stimLev [3] is statistically significant and positive (beta =
## 0.82, 95% CI [0.78, 0.85], t(362) = 44.89, p < .001; Std. beta = 1.89, 95% CI
## [1.81, 1.98])
##   - The effect of stimLev [4] is statistically significant and positive (beta =
## 0.94, 95% CI [0.90, 0.98], t(362) = 51.61, p < .001; Std. beta = 2.18, 95% CI
## [2.09, 2.26])
## 
## Standardized parameters were obtained by fitting the model on a standardized
## version of the dataset. 95% Confidence Intervals (CIs) and p-values were
## computed using a Wald t-distribution approximation.
model_VDT <- lmer(diff ~  isStrength + stimLev + (1 | sub), data = VDT_anova_grip_diff_combined)

summary(model_VDT)
## Linear mixed model fit by REML ['lmerMod']
## Formula: diff ~ isStrength + stimLev + (1 | sub)
##    Data: VDT_anova_grip_diff_combined
## 
## REML criterion at convergence: -308.3
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.5460 -0.6338  0.0243  0.6003  2.5044 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  sub      (Intercept) 0.01450  0.1204  
##  Residual             0.01887  0.1374  
## Number of obs: 370, groups:  sub, 37
## 
## Fixed effects:
##              Estimate Std. Error t value
## (Intercept)  0.096095   0.026415   3.638
## isStrength  -0.008551   0.014282  -0.599
## stimLev1     0.113553   0.022582   5.028
## stimLev2     0.354560   0.022582  15.701
## stimLev3     0.725719   0.022582  32.137
## stimLev4     0.867355   0.022582  38.408
## 
## Correlation of Fixed Effects:
##            (Intr) isStrn stmLv1 stmLv2 stmLv3
## isStrength -0.270                            
## stimLev1   -0.427  0.000                     
## stimLev2   -0.427  0.000  0.500              
## stimLev3   -0.427  0.000  0.500  0.500       
## stimLev4   -0.427  0.000  0.500  0.500  0.500
report(model_VDT)
## We fitted a linear mixed model (estimated using REML and nloptwrap optimizer)
## to predict diff with isStrength and stimLev (formula: diff ~ isStrength +
## stimLev). The model included sub as random effect (formula: ~1 | sub). The
## model's total explanatory power is substantial (conditional R2 = 0.87) and the
## part related to the fixed effects alone (marginal R2) is of 0.77. The model's
## intercept, corresponding to isStrength = 0 and stimLev = 0, is at 0.10 (95% CI
## [0.04, 0.15], t(362) = 3.64, p < .001). Within this model:
## 
##   - The effect of isStrength is statistically non-significant and negative (beta
## = -8.55e-03, 95% CI [-0.04, 0.02], t(362) = -0.60, p = 0.550; Std. beta =
## -0.01, 95% CI [-0.05, 0.03])
##   - The effect of stimLev [1] is statistically significant and positive (beta =
## 0.11, 95% CI [0.07, 0.16], t(362) = 5.03, p < .001; Std. beta = 0.30, 95% CI
## [0.18, 0.41])
##   - The effect of stimLev [2] is statistically significant and positive (beta =
## 0.35, 95% CI [0.31, 0.40], t(362) = 15.70, p < .001; Std. beta = 0.93, 95% CI
## [0.81, 1.04])
##   - The effect of stimLev [3] is statistically significant and positive (beta =
## 0.73, 95% CI [0.68, 0.77], t(362) = 32.14, p < .001; Std. beta = 1.90, 95% CI
## [1.78, 2.01])
##   - The effect of stimLev [4] is statistically significant and positive (beta =
## 0.87, 95% CI [0.82, 0.91], t(362) = 38.41, p < .001; Std. beta = 2.26, 95% CI
## [2.15, 2.38])
## 
## Standardized parameters were obtained by fitting the model on a standardized
## version of the dataset. 95% Confidence Intervals (CIs) and p-values were
## computed using a Wald t-distribution approximation.
model_CDT <- lmer(diff ~  isStrength + stimLev + (1 | sub), data = CDT_anova_grip_diff_combined)

summary(model_CDT)
## Linear mixed model fit by REML ['lmerMod']
## Formula: diff ~ isStrength + stimLev + (1 | sub)
##    Data: CDT_anova_grip_diff_combined
## 
## REML criterion at convergence: -356
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.1660 -0.6705 -0.0085  0.6975  2.8129 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  sub      (Intercept) 0.005241 0.07239 
##  Residual             0.017904 0.13381 
## Number of obs: 370, groups:  sub, 37
## 
## Fixed effects:
##              Estimate Std. Error t value
## (Intercept)  0.213543   0.020784  10.274
## isStrength  -0.023730   0.013912  -1.706
## stimLev1     0.009541   0.021997   0.434
## stimLev2     0.167854   0.021997   7.631
## stimLev3     0.513877   0.021997  23.361
## stimLev4     0.673384   0.021997  30.612
## 
## Correlation of Fixed Effects:
##            (Intr) isStrn stmLv1 stmLv2 stmLv3
## isStrength -0.335                            
## stimLev1   -0.529  0.000                     
## stimLev2   -0.529  0.000  0.500              
## stimLev3   -0.529  0.000  0.500  0.500       
## stimLev4   -0.529  0.000  0.500  0.500  0.500
report(model_CDT)
## We fitted a linear mixed model (estimated using REML and nloptwrap optimizer)
## to predict diff with isStrength and stimLev (formula: diff ~ isStrength +
## stimLev). The model included sub as random effect (formula: ~1 | sub). The
## model's total explanatory power is substantial (conditional R2 = 0.82) and the
## part related to the fixed effects alone (marginal R2) is of 0.76. The model's
## intercept, corresponding to isStrength = 0 and stimLev = 0, is at 0.21 (95% CI
## [0.17, 0.25], t(362) = 10.27, p < .001). Within this model:
## 
##   - The effect of isStrength is statistically non-significant and negative (beta
## = -0.02, 95% CI [-0.05, 3.63e-03], t(362) = -1.71, p = 0.089; Std. beta =
## -0.04, 95% CI [-0.08, 5.81e-03])
##   - The effect of stimLev [1] is statistically non-significant and positive (beta
## = 9.54e-03, 95% CI [-0.03, 0.05], t(362) = 0.43, p = 0.665; Std. beta = 0.03,
## 95% CI [-0.11, 0.17])
##   - The effect of stimLev [2] is statistically significant and positive (beta =
## 0.17, 95% CI [0.12, 0.21], t(362) = 7.63, p < .001; Std. beta = 0.54, 95% CI
## [0.40, 0.68])
##   - The effect of stimLev [3] is statistically significant and positive (beta =
## 0.51, 95% CI [0.47, 0.56], t(362) = 23.36, p < .001; Std. beta = 1.64, 95% CI
## [1.51, 1.78])
##   - The effect of stimLev [4] is statistically significant and positive (beta =
## 0.67, 95% CI [0.63, 0.72], t(362) = 30.61, p < .001; Std. beta = 2.15, 95% CI
## [2.02, 2.29])
## 
## Standardized parameters were obtained by fitting the model on a standardized
## version of the dataset. 95% Confidence Intervals (CIs) and p-values were
## computed using a Wald t-distribution approximation.
model_MST <- lmer(diff ~  isStrength + stimLev + (1 | sub), data = MST_anova_grip_old_combined)

summary(model_MST)
## Linear mixed model fit by REML ['lmerMod']
## Formula: diff ~ isStrength + stimLev + (1 | sub)
##    Data: MST_anova_grip_old_combined
## 
## REML criterion at convergence: -129.6
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.3489 -0.5921  0.0272  0.6627  2.5080 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  sub      (Intercept) 0.01781  0.1334  
##  Residual             0.03167  0.1780  
## Number of obs: 370, groups:  sub, 37
## 
## Fixed effects:
##             Estimate Std. Error t value
## (Intercept)  0.20078    0.03154   6.366
## isStrength  -0.02216    0.01850  -1.198
## stimLev1     0.17645    0.02925   6.031
## stimLev2     0.34675    0.02925  11.853
## stimLev3     0.46491    0.02925  15.892
## stimLev4     0.57516    0.02925  19.660
## 
## Correlation of Fixed Effects:
##            (Intr) isStrn stmLv1 stmLv2 stmLv3
## isStrength -0.293                            
## stimLev1   -0.464  0.000                     
## stimLev2   -0.464  0.000  0.500              
## stimLev3   -0.464  0.000  0.500  0.500       
## stimLev4   -0.464  0.000  0.500  0.500  0.500
report(model_MST)
## We fitted a linear mixed model (estimated using REML and nloptwrap optimizer)
## to predict diff with isStrength and stimLev (formula: diff ~ isStrength +
## stimLev). The model included sub as random effect (formula: ~1 | sub). The
## model's total explanatory power is substantial (conditional R2 = 0.65) and the
## part related to the fixed effects alone (marginal R2) is of 0.46. The model's
## intercept, corresponding to isStrength = 0 and stimLev = 0, is at 0.20 (95% CI
## [0.14, 0.26], t(362) = 6.37, p < .001). Within this model:
## 
##   - The effect of isStrength is statistically non-significant and negative (beta
## = -0.02, 95% CI [-0.06, 0.01], t(362) = -1.20, p = 0.232; Std. beta = -0.04,
## 95% CI [-0.10, 0.02])
##   - The effect of stimLev [1] is statistically significant and positive (beta =
## 0.18, 95% CI [0.12, 0.23], t(362) = 6.03, p < .001; Std. beta = 0.59, 95% CI
## [0.39, 0.78])
##   - The effect of stimLev [2] is statistically significant and positive (beta =
## 0.35, 95% CI [0.29, 0.40], t(362) = 11.85, p < .001; Std. beta = 1.15, 95% CI
## [0.96, 1.34])
##   - The effect of stimLev [3] is statistically significant and positive (beta =
## 0.46, 95% CI [0.41, 0.52], t(362) = 15.89, p < .001; Std. beta = 1.54, 95% CI
## [1.35, 1.73])
##   - The effect of stimLev [4] is statistically significant and positive (beta =
## 0.58, 95% CI [0.52, 0.63], t(362) = 19.66, p < .001; Std. beta = 1.91, 95% CI
## [1.72, 2.10])
## 
## Standardized parameters were obtained by fitting the model on a standardized
## version of the dataset. 95% Confidence Intervals (CIs) and p-values were
## computed using a Wald t-distribution approximation.

6.10.2 Effect of Grip on RT across tasks and stimulus levels

#MST_anova_grip_old_RT_combined <- MST_4T %>% group_by(stimlev_mst, isStrength, sub) %>%
#    #filter(resp1 == 'old') %>%
#    summarise(RT_sub = median(resp1RT), stimlev_mst, isStrength, sub) %>%
#    ungroup() %>% group_by(stimlev_mst) %>%
#    summarise(RT_old = median(RT_sub), stimlev_mst, isStrength, sub) %>%
#  distinct()
#MST_anova_grip_old_RT_combined$stimlev_mst <- as.factor(MST_anova_grip_old_RT_combined$stimlev_mst)
#MST_anova_grip_old_RT_combined$stimlev_mst <- recode_factor(MST_anova_grip_old_RT_combined$stimlev_mst,'1lure'="1", '2lure'="2", '3lure'="3", '4lure'="4")
#MST_anova_grip_old_RT_combined <- MST_anova_grip_old_RT_combined %>% filter(stimlev_mst!='0targ') %>% filter(stimlev_mst!='5foil')
#MST_anova_grip_old_RT_combined <- rename(MST_anova_grip_old_RT_combined, stimLev = stimlev_mst)
#MST_anova_grip_old_RT_combined <- rename(MST_anova_grip_old_RT_combined, RT = RT_old)
#MST_anova_grip_old_RT_combined$task <- "MST"


ADT_anova_grip_diff_RT_combined <- ADT_4T %>% group_by(stimLev, isStrength, sub) %>%
  filter(iscorr == 1) %>%
  summarise(RT_sub = median(resp1RT), stimLev, isStrength, sub) %>%
  ungroup() %>% group_by(stimLev) %>%
  summarise(RT = median(RT_sub), stimLev, isStrength, sub) %>%
  distinct()
ADT_anova_grip_diff_RT_combined$stimLev <- as.factor(ADT_anova_grip_diff_RT_combined$stimLev)
ADT_anova_grip_diff_RT_combined$stimLev <- recode_factor(ADT_anova_grip_diff_RT_combined$stimLev, '0'="0",'4'="1", '8'="2", '32'="3", '128'="4")
ADT_anova_grip_diff_RT_combined$task <- "ADT"

VDT_anova_grip_diff_RT_combined <- VDT_4T %>% group_by(stimLev, isStrength, sub) %>%
    filter(iscorr == 1) %>%
  summarise(RT_sub = median(resp1RT), stimLev, isStrength, sub) %>%
  ungroup() %>% group_by(stimLev) %>%
  summarise(RT = median(RT_sub), stimLev, isStrength, sub) %>%
  distinct()
VDT_anova_grip_diff_RT_combined$stimLev <- as.factor(VDT_anova_grip_diff_RT_combined$stimLev)
VDT_anova_grip_diff_RT_combined$stimLev <- recode_factor(VDT_anova_grip_diff_RT_combined$stimLev, '0'="0", '0.04'="1", '0.08'="2", '0.16'="3", '0.32'="4")
VDT_anova_grip_diff_RT_combined$task <- "VDT"

CDT_anova_grip_diff_RT_combined <- CDT_4T %>% group_by(stimLev, isStrength, sub) %>%
    filter(iscorr == 1) %>%
  summarise(RT_sub = median(resp1RT), stimLev, isStrength, sub) %>%
  ungroup() %>% group_by(stimLev) %>%
  summarise(RT = median(RT_sub), stimLev, isStrength, sub) %>%
  distinct()
CDT_anova_grip_diff_RT_combined$stimLev <- as.factor(CDT_anova_grip_diff_RT_combined$stimLev)
CDT_anova_grip_diff_RT_combined$stimLev <- recode_factor(CDT_anova_grip_diff_RT_combined$stimLev,'0'="0",'5'="1", '20'="2", '45'="3", '90'="4")
CDT_anova_grip_diff_RT_combined$task <- "CDT"

alltask_anova_grip_diff_RT <- rbind(ADT_anova_grip_diff_RT_combined,VDT_anova_grip_diff_RT_combined,CDT_anova_grip_diff_RT_combined)
alltask_anova_grip_diff_RT$isStrength <- as.factor(alltask_anova_grip_diff_RT$isStrength)
alltask_anova_grip_diff_RT$task <- as.factor(alltask_anova_grip_diff_RT$task)


library(lme4)
model_RT_ADT <- lmer(RT ~ isStrength + stimLev + (1 | sub), data = ADT_anova_grip_diff_RT_combined)

summary(model_RT_ADT)
## Linear mixed model fit by REML ['lmerMod']
## Formula: RT ~ isStrength + stimLev + (1 | sub)
##    Data: ADT_anova_grip_diff_RT_combined
## 
## REML criterion at convergence: -22824.6
## 
## Scaled residuals: 
##    Min     1Q Median     3Q    Max 
## -1.345  0.000  0.000  1.345  1.345 
## 
## Random effects:
##  Groups   Name        Variance  Std.Dev. 
##  sub      (Intercept) 3.848e-34 1.962e-17
##  Residual             6.814e-33 8.255e-17
## Number of obs: 327, groups:  sub, 37
## 
## Fixed effects:
##               Estimate Std. Error    t value
## (Intercept)  4.057e-01  1.111e-17  3.653e+16
## isStrength   8.945e-17  9.135e-18  9.793e+00
## stimLev1     2.470e-01  1.635e-17  1.511e+16
## stimLev2     1.824e-01  1.406e-17  1.297e+16
## stimLev3    -5.293e-02  1.357e-17 -3.900e+15
## stimLev4    -7.637e-02  1.357e-17 -5.627e+15
## 
## Correlation of Fixed Effects:
##            (Intr) isStrn stmLv1 stmLv2 stmLv3
## isStrength -0.411                            
## stimLev1   -0.507  0.000                     
## stimLev2   -0.588 -0.005  0.405              
## stimLev3   -0.611  0.000  0.415  0.482       
## stimLev4   -0.611  0.000  0.415  0.482  0.500
## optimizer (nloptwrap) convergence code: 0 (OK)
## Model failed to converge with max|grad| = 1.8417 (tol = 0.002, component 1)
## Model is nearly unidentifiable: very large eigenvalue
##  - Rescale variables?
report(model_RT_ADT)
## We fitted a linear mixed model (estimated using REML and nloptwrap optimizer)
## to predict RT with isStrength and stimLev (formula: RT ~ isStrength + stimLev).
## The model included sub as random effect (formula: ~1 | sub). The model's total
## explanatory power is substantial (conditional R2 = 1.00) and the part related
## to the fixed effects alone (marginal R2) is of 1.00. The model's intercept,
## corresponding to isStrength = 0 and stimLev = 0, is at 0.41 (95% CI [0.41,
## 0.41], t(319) = 3.65e+16, p < .001). Within this model:
## 
##   - The effect of isStrength is statistically significant and positive (beta =
## 8.95e-17, 95% CI [7.15e-17, 1.07e-16], t(319) = 9.79, p < .001; Std. beta =
## -1.51e-16, 95% CI [-2.41e-16, -6.10e-17])
##   - The effect of stimLev [1] is statistically significant and positive (beta =
## 0.25, 95% CI [0.25, 0.25], t(319) = 1.51e+16, p < .001; Std. beta = 2.04, 95%
## CI [2.04, 2.04])
##   - The effect of stimLev [2] is statistically significant and positive (beta =
## 0.18, 95% CI [0.18, 0.18], t(319) = 1.30e+16, p < .001; Std. beta = 1.50, 95%
## CI [1.50, 1.50])
##   - The effect of stimLev [3] is statistically significant and negative (beta =
## -0.05, 95% CI [-0.05, -0.05], t(319) = -3.90e+15, p < .001; Std. beta = -0.44,
## 95% CI [-0.44, -0.44])
##   - The effect of stimLev [4] is statistically significant and negative (beta =
## -0.08, 95% CI [-0.08, -0.08], t(319) = -5.63e+15, p < .001; Std. beta = -0.63,
## 95% CI [-0.63, -0.63])
## 
## Standardized parameters were obtained by fitting the model on a standardized
## version of the dataset. 95% Confidence Intervals (CIs) and p-values were
## computed using a Wald t-distribution approximation.
model_RT_VDT <- lmer(RT ~  isStrength + stimLev + (1 | sub), data = VDT_anova_grip_diff_RT_combined)

summary(model_RT_VDT)
## Linear mixed model fit by REML ['lmerMod']
## Formula: RT ~ isStrength + stimLev + (1 | sub)
##    Data: VDT_anova_grip_diff_RT_combined
## 
## REML criterion at convergence: -26245.4
## 
## Scaled residuals: 
##    Min     1Q Median     3Q    Max 
##  0.000  0.000  0.000  0.000  2.193 
## 
## Random effects:
##  Groups   Name        Variance  Std.Dev. 
##  sub      (Intercept) 2.170e-36 1.473e-18
##  Residual             6.408e-34 2.531e-17
## Number of obs: 363, groups:  sub, 37
## 
## Fixed effects:
##               Estimate Std. Error    t value
## (Intercept)  3.823e-01  3.275e-18  1.167e+17
## isStrength   2.631e-17  2.657e-18  9.902e+00
## stimLev1     9.999e-02  4.265e-18  2.344e+16
## stimLev2     3.527e-02  4.191e-18  8.417e+15
## stimLev3    -6.467e-02  4.191e-18 -1.543e+16
## stimLev4    -6.469e-02  4.191e-18 -1.544e+16
## 
## Correlation of Fixed Effects:
##            (Intr) isStrn stmLv1 stmLv2 stmLv3
## isStrength -0.406                            
## stimLev1   -0.635 -0.005                     
## stimLev2   -0.649  0.000  0.498              
## stimLev3   -0.649  0.000  0.498  0.507       
## stimLev4   -0.649  0.000  0.498  0.507  0.507
## optimizer (nloptwrap) convergence code: 0 (OK)
## Model failed to converge with max|grad| = 6.98786 (tol = 0.002, component 1)
## Model is nearly unidentifiable: very large eigenvalue
##  - Rescale variables?
report(model_RT_VDT)
## We fitted a linear mixed model (estimated using REML and nloptwrap optimizer)
## to predict RT with isStrength and stimLev (formula: RT ~ isStrength + stimLev).
## The model included sub as random effect (formula: ~1 | sub). The model's total
## explanatory power is substantial (conditional R2 = 1.00) and the part related
## to the fixed effects alone (marginal R2) is of 1.00. The model's intercept,
## corresponding to isStrength = 0 and stimLev = 0, is at 0.38 (95% CI [0.38,
## 0.38], t(355) = 1.17e+17, p < .001). Within this model:
## 
##   - The effect of isStrength is statistically significant and positive (beta =
## 2.63e-17, 95% CI [2.11e-17, 3.15e-17], t(355) = 9.90, p < .001; Std. beta =
## 1.31e-17, 95% CI [-2.89e-16, 3.15e-16])
##   - The effect of stimLev [1] is statistically significant and positive (beta =
## 0.10, 95% CI [0.10, 0.10], t(355) = 2.34e+16, p < .001; Std. beta = 1.61, 95%
## CI [1.61, 1.61])
##   - The effect of stimLev [2] is statistically significant and positive (beta =
## 0.04, 95% CI [0.04, 0.04], t(355) = 8.42e+15, p < .001; Std. beta = 0.57, 95%
## CI [0.57, 0.57])
##   - The effect of stimLev [3] is statistically significant and negative (beta =
## -0.06, 95% CI [-0.06, -0.06], t(355) = -1.54e+16, p < .001; Std. beta = -1.04,
## 95% CI [-1.04, -1.04])
##   - The effect of stimLev [4] is statistically significant and negative (beta =
## -0.06, 95% CI [-0.06, -0.06], t(355) = -1.54e+16, p < .001; Std. beta = -1.04,
## 95% CI [-1.04, -1.04])
## 
## Standardized parameters were obtained by fitting the model on a standardized
## version of the dataset. 95% Confidence Intervals (CIs) and p-values were
## computed using a Wald t-distribution approximation.
model_RT_CDT <- lmer(RT ~ isStrength + stimLev + (1 | sub), data = CDT_anova_grip_diff_RT_combined)

summary(model_RT_CDT)
## Linear mixed model fit by REML ['lmerMod']
## Formula: RT ~ isStrength + stimLev + (1 | sub)
##    Data: CDT_anova_grip_diff_RT_combined
## 
## REML criterion at convergence: -23389.5
## 
## Scaled residuals: 
##    Min     1Q Median     3Q    Max 
## -1.534  0.000  0.000  0.000  1.534 
## 
## Random effects:
##  Groups   Name        Variance  Std.Dev. 
##  sub      (Intercept) 0.000e+00 0.000e+00
##  Residual             2.096e-32 1.448e-16
## Number of obs: 340, groups:  sub, 37
## 
## Fixed effects:
##               Estimate Std. Error    t value
## (Intercept)  1.606e+00  1.914e-17  8.388e+16
## isStrength  -6.021e-18  1.571e-17 -3.830e-01
## stimLev1    -2.177e-01  2.432e-17 -8.950e+15
## stimLev2    -1.707e-01  2.432e-17 -7.018e+15
## stimLev3    -1.176e-01  2.448e-17 -4.805e+15
## stimLev4    -1.472e-01  2.668e-17 -5.517e+15
## 
## Correlation of Fixed Effects:
##            (Intr) isStrn stmLv1 stmLv2 stmLv3
## isStrength -0.398                            
## stimLev1   -0.658 -0.009                     
## stimLev2   -0.658 -0.009  0.521              
## stimLev3   -0.654 -0.009  0.518  0.518       
## stimLev4   -0.596 -0.020  0.475  0.475  0.472
## optimizer (nloptwrap) convergence code: 0 (OK)
## boundary (singular) fit: see help('isSingular')
report(model_RT_CDT)
## Random effect variances not available. Returned R2 does not account for random effects.
## Random effect variances not available. Returned R2 does not account for random effects.
## We fitted a linear mixed model (estimated using REML and nloptwrap optimizer)
## to predict RT with isStrength and stimLev (formula: RT ~ isStrength + stimLev).
## The model included sub as random effect (formula: ~1 | sub). The model's
## explanatory power related to the fixed effects alone (marginal R2) is 1.00. The
## model's intercept, corresponding to isStrength = 0 and stimLev = 0, is at 1.61
## (95% CI [1.61, 1.61], t(332) = 8.39e+16, p < .001). Within this model:
## 
##   - The effect of isStrength is statistically non-significant and negative (beta
## = -6.02e-18, 95% CI [-3.69e-17, 2.49e-17], t(332) = -0.38, p = 0.702; Std. beta
## = -1.06e-16, 95% CI [-1.57e-16, -5.40e-17])
##   - The effect of stimLev [1] is statistically significant and negative (beta =
## -0.22, 95% CI [-0.22, -0.22], t(332) = -8.95e+15, p < .001; Std. beta = -2.93,
## 95% CI [-2.93, -2.93])
##   - The effect of stimLev [2] is statistically significant and negative (beta =
## -0.17, 95% CI [-0.17, -0.17], t(332) = -7.02e+15, p < .001; Std. beta = -2.30,
## 95% CI [-2.30, -2.30])
##   - The effect of stimLev [3] is statistically significant and negative (beta =
## -0.12, 95% CI [-0.12, -0.12], t(332) = -4.80e+15, p < .001; Std. beta = -1.59,
## 95% CI [-1.59, -1.59])
##   - The effect of stimLev [4] is statistically significant and negative (beta =
## -0.15, 95% CI [-0.15, -0.15], t(332) = -5.52e+15, p < .001; Std. beta = -1.98,
## 95% CI [-1.98, -1.98])
## 
## Standardized parameters were obtained by fitting the model on a standardized
## version of the dataset. 95% Confidence Intervals (CIs) and p-values were
## computed using a Wald t-distribution approximation.
# Install and load the emmeans package
#install.packages("emmeans")
library(emmeans)

# Perform post-hoc comparisons for the isStrength variable
isStrength_emm <- emmeans(model_RT_CDT, ~ isStrength)

# Summarize the pairwise comparisons
summary(isStrength_emm)
# Plot the pairwise comparisons
plot(isStrength_emm)

6.10.3 Effect of Grip on Targets across tasks

alltask_anova_grip_diff_stim0 <- alltask_anova_grip_diff %>%
  filter(stimLev == '0')

model_stim0 <- lmer(diff ~ task + isStrength + (1 | sub), data = alltask_anova_grip_diff_stim0)

# Print the summary of the model
summary(model_stim0)
## Linear mixed model fit by REML ['lmerMod']
## Formula: diff ~ task + isStrength + (1 | sub)
##    Data: alltask_anova_grip_diff_stim0
## 
## REML criterion at convergence: -459
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -2.0011 -0.6374 -0.0876  0.3720  4.5908 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  sub      (Intercept) 0.002033 0.04509 
##  Residual             0.009963 0.09981 
## Number of obs: 296, groups:  sub, 37
## 
## Fixed effects:
##              Estimate Std. Error t value
## (Intercept)  0.030214   0.014941   2.022
## taskCDT      0.172213   0.016409  10.495
## taskMST      0.160234   0.016409   9.765
## taskVDT      0.062353   0.016409   3.800
## isStrength1 -0.001498   0.011603  -0.129
## 
## Correlation of Fixed Effects:
##             (Intr) tskCDT tskMST tskVDT
## taskCDT     -0.549                     
## taskMST     -0.549  0.500              
## taskVDT     -0.549  0.500  0.500       
## isStrength1 -0.388  0.000  0.000  0.000
report(model_stim0)
## We fitted a linear mixed model (estimated using REML and nloptwrap optimizer)
## to predict diff with task and isStrength (formula: diff ~ task + isStrength).
## The model included sub as random effect (formula: ~1 | sub). The model's total
## explanatory power is substantial (conditional R2 = 0.42) and the part related
## to the fixed effects alone (marginal R2) is of 0.30. The model's intercept,
## corresponding to task = ADT and isStrength = 0, is at 0.03 (95% CI [8.08e-04,
## 0.06], t(289) = 2.02, p = 0.044). Within this model:
## 
##   - The effect of task [CDT] is statistically significant and positive (beta =
## 0.17, 95% CI [0.14, 0.20], t(289) = 10.49, p < .001; Std. beta = 1.32, 95% CI
## [1.08, 1.57])
##   - The effect of task [MST] is statistically significant and positive (beta =
## 0.16, 95% CI [0.13, 0.19], t(289) = 9.76, p < .001; Std. beta = 1.23, 95% CI
## [0.98, 1.48])
##   - The effect of task [VDT] is statistically significant and positive (beta =
## 0.06, 95% CI [0.03, 0.09], t(289) = 3.80, p < .001; Std. beta = 0.48, 95% CI
## [0.23, 0.73])
##   - The effect of isStrength [1] is statistically non-significant and negative
## (beta = -1.50e-03, 95% CI [-0.02, 0.02], t(289) = -0.13, p = 0.897; Std. beta =
## -0.01, 95% CI [-0.19, 0.16])
## 
## Standardized parameters were obtained by fitting the model on a standardized
## version of the dataset. 95% Confidence Intervals (CIs) and p-values were
## computed using a Wald t-distribution approximation.

6.10.4 Effect of Grip on RT and stimulus levels using MLM

6.10.4.1 ADT

ADT_anova_grip_diff_RT_MLM <- ADT_4T %>% group_by(stimLev, auc_rel_mvc, sub, resp1_diff) %>%
  filter(stimLev != '0') %>%
  summarise(RT_sub = resp1RT, stimLev, auc_rel_mvc, sub, resp1_diff) %>%
  ungroup() %>% group_by(stimLev) %>%
  summarise(RT = RT_sub, stimLev, auc_rel_mvc, sub, resp1_diff) %>%
  distinct()
ADT_anova_grip_diff_RT_MLM$stimLev <- as.factor(ADT_anova_grip_diff_RT_MLM$stimLev)
ADT_anova_grip_diff_RT_MLM$stimLev <- recode_factor(ADT_anova_grip_diff_RT_MLM$stimLev,'4'="1", '8'="2", '32'="3", '128'="4")
ADT_anova_grip_diff_RT_MLM$task <- "ADT"


model5 <- lmer(RT ~ auc_rel_mvc * stimLev + (1 | sub), data = ADT_anova_grip_diff_RT_MLM)
summary(model5)
## Linear mixed model fit by REML ['lmerMod']
## Formula: RT ~ auc_rel_mvc * stimLev + (1 | sub)
##    Data: ADT_anova_grip_diff_RT_MLM
## 
## REML criterion at convergence: 9360.1
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -2.4052 -0.6360 -0.2581  0.3374  5.5812 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  sub      (Intercept) 0.05999  0.2449  
##  Residual             0.20582  0.4537  
## Number of obs: 7304, groups:  sub, 37
## 
## Fixed effects:
##                      Estimate Std. Error t value
## (Intercept)           0.59461    0.04498  13.219
## auc_rel_mvc          -0.15897    0.10877  -1.461
## stimLev2              0.03409    0.02828   1.206
## stimLev3             -0.03646    0.02841  -1.283
## stimLev4             -0.11797    0.02826  -4.174
## auc_rel_mvc:stimLev2  0.06214    0.15293   0.406
## auc_rel_mvc:stimLev3  0.12622    0.15396   0.820
## auc_rel_mvc:stimLev4 -0.02199    0.15322  -0.143
## 
## Correlation of Fixed Effects:
##             (Intr) ac_rl_ stmLv2 stmLv3 stmLv4 a__:L2 a__:L3
## auc_rel_mvc -0.378                                          
## stimLev2    -0.315  0.598                                   
## stimLev3    -0.313  0.595  0.499                            
## stimLev4    -0.315  0.599  0.501  0.499                     
## ac_rl_mv:L2  0.268 -0.708 -0.847 -0.424 -0.426              
## ac_rl_mv:L3  0.266 -0.703 -0.423 -0.848 -0.423  0.500       
## ac_rl_mv:L4  0.267 -0.706 -0.425 -0.423 -0.847  0.502  0.499
report(model5)
## We fitted a linear mixed model (estimated using REML and nloptwrap optimizer)
## to predict RT with auc_rel_mvc and stimLev (formula: RT ~ auc_rel_mvc *
## stimLev). The model included sub as random effect (formula: ~1 | sub). The
## model's total explanatory power is moderate (conditional R2 = 0.24) and the
## part related to the fixed effects alone (marginal R2) is of 0.01. The model's
## intercept, corresponding to auc_rel_mvc = 0 and stimLev = 1, is at 0.59 (95% CI
## [0.51, 0.68], t(7294) = 13.22, p < .001). Within this model:
## 
##   - The effect of auc rel mvc is statistically non-significant and negative (beta
## = -0.16, 95% CI [-0.37, 0.05], t(7294) = -1.46, p = 0.144; Std. beta = -0.03,
## 95% CI [-0.07, 0.01])
##   - The effect of stimLev [2] is statistically non-significant and positive (beta
## = 0.03, 95% CI [-0.02, 0.09], t(7294) = 1.21, p = 0.228; Std. beta = 0.08, 95%
## CI [0.03, 0.14])
##   - The effect of stimLev [3] is statistically non-significant and negative (beta
## = -0.04, 95% CI [-0.09, 0.02], t(7294) = -1.28, p = 0.199; Std. beta = -0.03,
## 95% CI [-0.09, 0.02])
##   - The effect of stimLev [4] is statistically significant and negative (beta =
## -0.12, 95% CI [-0.17, -0.06], t(7294) = -4.17, p < .001; Std. beta = -0.23, 95%
## CI [-0.29, -0.18])
##   - The effect of auc rel mvc × stimLev [2] is statistically non-significant and
## positive (beta = 0.06, 95% CI [-0.24, 0.36], t(7294) = 0.41, p = 0.685; Std.
## beta = 0.01, 95% CI [-0.04, 0.07])
##   - The effect of auc rel mvc × stimLev [3] is statistically non-significant and
## positive (beta = 0.13, 95% CI [-0.18, 0.43], t(7294) = 0.82, p = 0.412; Std.
## beta = 0.02, 95% CI [-0.03, 0.08])
##   - The effect of auc rel mvc × stimLev [4] is statistically non-significant and
## negative (beta = -0.02, 95% CI [-0.32, 0.28], t(7294) = -0.14, p = 0.886; Std.
## beta = -4.16e-03, 95% CI [-0.06, 0.05])
## 
## Standardized parameters were obtained by fitting the model on a standardized
## version of the dataset. 95% Confidence Intervals (CIs) and p-values were
## computed using a Wald t-distribution approximation.
# Interaction plot
interaction_plot5 <- ggplot(ADT_anova_grip_diff_RT_MLM, aes(x = auc_rel_mvc, y = RT, color = factor(stimLev))) +
  geom_line(aes(group = factor(stimLev)), size = 1) +
  geom_point(size = 3) +
  labs(x = "auc_rel_mvc", y = "RT", color = "stimLev") +
  theme_minimal()

# Display the plot
interaction_plot5

# Load necessary libraries
library(sjPlot)

# Marginal effects plot
marginal_plot5 <- plot_model(model5, type = "eff", terms = c("auc_rel_mvc", "stimLev"))

# Display the plot
print(marginal_plot5)

6.10.4.1.1 Cluster analysis
# Assuming your dataset is named 'my_data' and contains the relevant variables for clustering
# Replace 'RT' and 'auc_rel_mvc' with the actual column names from your dataset

# Compute the within-cluster sum of squares (WSS) for different values of k
wss <- numeric(10)  # assuming a maximum of 10 clusters
for (i in 1:10) {
  kmeans_model <- kmeans(ADT_anova_grip_diff_RT_MLM[, c("RT", "auc_rel_mvc")], centers = i)
  wss[i] <- sum(kmeans_model$withinss)
}

# Plot the elbow curve
plot(1:10, wss, type = "b", pch = 19, frame = FALSE, 
     xlab = "Number of clusters",
     ylab = "Within-cluster sum of squares",
     main = "Elbow Method for Optimal Number of Clusters")

# Perform k-means clustering with 2 clusters
k <- 4
kmeans_model <- kmeans(ADT_anova_grip_diff_RT_MLM[, c("RT", "auc_rel_mvc", "stimLev")], centers = k)
clusters <- kmeans_model$cluster


# Visualize the clusters
library(ggplot2)
ADT_cluster <- ggplot(ADT_anova_grip_diff_RT_MLM, aes(x = RT, y = auc_rel_mvc, color = factor(kmeans_model$cluster))) +
  geom_point() +
  labs(x = "RT", y = "auc_rel_mvc", color = "Cluster") +
  ggtitle("K-means Clustering (k = 4)") +
  geom_jitter()

ggplotly(ADT_cluster)
# Add cluster assignments to the original dataset
ADT_anova_grip_diff_RT_MLM$cluster <- as.factor(clusters)

# Calculate cluster centroids
cluster_centroids <- aggregate(cbind(RT, auc_rel_mvc) ~ cluster, data = ADT_anova_grip_diff_RT_MLM, FUN = mean)

# Visualize cluster centroids
ADT_cluster_centroids <- ggplot(ADT_anova_grip_diff_RT_MLM, aes(x = RT, y = auc_rel_mvc, color = factor(cluster))) +
  geom_point(alpha = 0.5) +
  geom_point(data = cluster_centroids, aes(x = RT, y = auc_rel_mvc, color = factor(cluster)), size = 4, shape = 18) +
  labs(x = "RT", y = "auc_rel_mvc", color = "Cluster") +
  ggtitle("Cluster Centroids — ADT") +
  geom_jitter()

ggplotly(ADT_cluster_centroids)
# Calculate within-cluster sum of squares
wss <- sum(kmeans_model$withinss)

# Print the total within-cluster sum of squares
print(paste("Total within-cluster sum of squares:", wss))
## [1] "Total within-cluster sum of squares: 2160.69895439534"
# Explore the relationship between clusters and stimulus levels
cluster_stimulus <- table(ADT_anova_grip_diff_RT_MLM$cluster, ADT_anova_grip_diff_RT_MLM$stimLev)
print(cluster_stimulus)
##    
##        1    2    3    4
##   1   58  456  289   10
##   2    0 1375 1529    0
##   3 1764    0    0    0
##   4    0    0    0 1823
# Perform association rule mining
library(arules)
data <- ADT_anova_grip_diff_RT_MLM[, c("RT", "auc_rel_mvc")]
transactions <- as(data, "transactions")
rules <- apriori(transactions, parameter = list(supp = 0.1, conf = 0.8))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.8    0.1    1 none FALSE            TRUE       5     0.1      1
##  maxlen target  ext
##      10  rules TRUE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 730 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[6 item(s), 7304 transaction(s)] done [0.00s].
## sorting and recoding items ... [6 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 done [0.00s].
## writing ... [0 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
# Print the discovered rules
inspect(rules)
6.10.4.1.2 Time series analysis
# Descriptive statistics
summary(ADT_anova_grip_diff_RT_MLM$RT)
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
## 0.009718 0.188130 0.399966 0.544334 0.752779 3.010981
sd(ADT_anova_grip_diff_RT_MLM$RT)
## [1] 0.517453
# Time series plot
plot(ADT_anova_grip_diff_RT_MLM$stimLev, ADT_anova_grip_diff_RT_MLM$RT, type = "l", xlab = "Stimulus Level", ylab = "RT", main = "RT vs Stimulus Level")

# STL decomposition
#decomposed <- stl(ADT_anova_grip_diff_RT_MLM$RT, s.window = "periodic")
#plot(decomposed)
# Pearson correlation
correlation_matrix <- cor(ADT_anova_grip_diff_RT_MLM$RT, ADT_anova_grip_diff_RT_MLM$auc_rel_mvc, method = "pearson")

print(correlation_matrix)
## [1] -0.03652963
# ARIMA model
arima_model <- forecast::auto.arima(ADT_anova_grip_diff_RT_MLM$RT)
forecast_values <- forecast::forecast(arima_model)
plot(forecast_values)

# Change point detection (example using changepoint package)
library(changepoint)
cpt <- cpt.mean(ADT_anova_grip_diff_RT_MLM$RT)
plot(cpt)

# Example code for Fourier transform
fourier_transform <- fft(ADT_anova_grip_diff_RT_MLM$RT)
plot(abs(fourier_transform))

6.10.4.2 VDT

VDT_anova_grip_diff_RT_MLM <- VDT_4T %>% group_by(stimLev, auc_rel_mvc, sub) %>%
  filter(stimLev != '0') %>%
  summarise(RT_sub = resp1RT, stimLev, auc_rel_mvc, sub) %>%
  ungroup() %>% group_by(stimLev) %>%
  summarise(RT = RT_sub, stimLev, auc_rel_mvc, sub) %>%
  distinct()
VDT_anova_grip_diff_RT_MLM$stimLev <- as.factor(VDT_anova_grip_diff_RT_MLM$stimLev)
VDT_anova_grip_diff_RT_MLM$stimLev <- recode_factor(VDT_anova_grip_diff_RT_MLM$stimLev,'0.04'="1", '0.08'="2", '0.16'="3", '0.32'="4")
VDT_anova_grip_diff_RT_MLM$task <- "VDT"


model6 <- lmer(RT ~ auc_rel_mvc * stimLev + (1 | sub), data = VDT_anova_grip_diff_RT_MLM)
summary(model6)
## Linear mixed model fit by REML ['lmerMod']
## Formula: RT ~ auc_rel_mvc * stimLev + (1 | sub)
##    Data: VDT_anova_grip_diff_RT_MLM
## 
## REML criterion at convergence: 7812.2
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -2.7249 -0.5692 -0.2102  0.3068  6.1693 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  sub      (Intercept) 0.07377  0.2716  
##  Residual             0.16634  0.4079  
## Number of obs: 7295, groups:  sub, 37
## 
## Fixed effects:
##                      Estimate Std. Error t value
## (Intercept)           0.55805    0.04823  11.570
## auc_rel_mvc          -0.01734    0.09274  -0.187
## stimLev2              0.02223    0.02583   0.861
## stimLev3             -0.02342    0.02580  -0.908
## stimLev4             -0.08883    0.02585  -3.436
## auc_rel_mvc:stimLev2  0.10265    0.13107   0.783
## auc_rel_mvc:stimLev3 -0.01916    0.13063  -0.147
## auc_rel_mvc:stimLev4 -0.05257    0.13124  -0.401
## 
## Correlation of Fixed Effects:
##             (Intr) ac_rl_ stmLv2 stmLv3 stmLv4 a__:L2 a__:L3
## auc_rel_mvc -0.322                                          
## stimLev2    -0.266  0.599                                   
## stimLev3    -0.267  0.599  0.498                            
## stimLev4    -0.266  0.598  0.497  0.497                     
## ac_rl_mv:L2  0.227 -0.704 -0.852 -0.424 -0.423              
## ac_rl_mv:L3  0.228 -0.706 -0.425 -0.852 -0.425  0.500       
## ac_rl_mv:L4  0.227 -0.703 -0.423 -0.424 -0.853  0.498  0.499
report(model6)
## We fitted a linear mixed model (estimated using REML and nloptwrap optimizer)
## to predict RT with auc_rel_mvc and stimLev (formula: RT ~ auc_rel_mvc *
## stimLev). The model included sub as random effect (formula: ~1 | sub). The
## model's total explanatory power is substantial (conditional R2 = 0.31) and the
## part related to the fixed effects alone (marginal R2) is of 0.01. The model's
## intercept, corresponding to auc_rel_mvc = 0 and stimLev = 1, is at 0.56 (95% CI
## [0.46, 0.65], t(7285) = 11.57, p < .001). Within this model:
## 
##   - The effect of auc rel mvc is statistically non-significant and negative (beta
## = -0.02, 95% CI [-0.20, 0.16], t(7285) = -0.19, p = 0.852; Std. beta =
## -3.65e-03, 95% CI [-0.04, 0.03])
##   - The effect of stimLev [2] is statistically non-significant and positive (beta
## = 0.02, 95% CI [-0.03, 0.07], t(7285) = 0.86, p = 0.389; Std. beta = 0.08, 95%
## CI [0.03, 0.13])
##   - The effect of stimLev [3] is statistically non-significant and negative (beta
## = -0.02, 95% CI [-0.07, 0.03], t(7285) = -0.91, p = 0.364; Std. beta = -0.05,
## 95% CI [-0.11, -3.12e-04])
##   - The effect of stimLev [4] is statistically significant and negative (beta =
## -0.09, 95% CI [-0.14, -0.04], t(7285) = -3.44, p < .001; Std. beta = -0.20, 95%
## CI [-0.25, -0.15])
##   - The effect of auc rel mvc × stimLev [2] is statistically non-significant and
## positive (beta = 0.10, 95% CI [-0.15, 0.36], t(7285) = 0.78, p = 0.434; Std.
## beta = 0.02, 95% CI [-0.03, 0.08])
##   - The effect of auc rel mvc × stimLev [3] is statistically non-significant and
## negative (beta = -0.02, 95% CI [-0.28, 0.24], t(7285) = -0.15, p = 0.883; Std.
## beta = -4.04e-03, 95% CI [-0.06, 0.05])
##   - The effect of auc rel mvc × stimLev [4] is statistically non-significant and
## negative (beta = -0.05, 95% CI [-0.31, 0.20], t(7285) = -0.40, p = 0.689; Std.
## beta = -0.01, 95% CI [-0.07, 0.04])
## 
## Standardized parameters were obtained by fitting the model on a standardized
## version of the dataset. 95% Confidence Intervals (CIs) and p-values were
## computed using a Wald t-distribution approximation.
# Interaction plot
interaction_plot6 <- ggplot(ADT_anova_grip_diff_RT_MLM, aes(x = auc_rel_mvc, y = RT, color = factor(stimLev))) +
  geom_line(aes(group = factor(stimLev)), size = 1) +
  geom_point(size = 3) +
  labs(x = "auc_rel_mvc", y = "RT", color = "stimLev") +
  theme_minimal()

# Display the plot
interaction_plot6

# Load necessary libraries
library(sjPlot)

# Marginal effects plot
marginal_plot6 <- plot_model(model6, type = "eff", terms = c("auc_rel_mvc", "stimLev"))

# Display the plot
print(marginal_plot6)

6.10.4.2.1 Cluster analysis
# Assuming your dataset is named 'my_data' and contains the relevant variables for clustering
# Replace 'RT' and 'auc_rel_mvc' with the actual column names from your dataset

# Compute the within-cluster sum of squares (WSS) for different values of k
wss <- numeric(10)  # assuming a maximum of 10 clusters
for (i in 1:10) {
  kmeans_model <- kmeans(VDT_anova_grip_diff_RT_MLM[, c("RT", "auc_rel_mvc")], centers = i)
  wss[i] <- sum(kmeans_model$withinss)
}

# Plot the elbow curve
plot(1:10, wss, type = "b", pch = 19, frame = FALSE, 
     xlab = "Number of clusters",
     ylab = "Within-cluster sum of squares",
     main = "Elbow Method for Optimal Number of Clusters")

# Perform k-means clustering with 2 clusters
k <- 4
kmeans_model <- kmeans(VDT_anova_grip_diff_RT_MLM[, c("RT", "auc_rel_mvc", "stimLev")], centers = k)
clusters <- kmeans_model$cluster


# Visualize the clusters
library(ggplot2)
VDT_cluster <- ggplot(VDT_anova_grip_diff_RT_MLM, aes(x = RT, y = auc_rel_mvc, color = factor(kmeans_model$cluster))) +
  geom_point() +
  labs(x = "RT", y = "auc_rel_mvc", color = "Cluster") +
  ggtitle("K-means Clustering (k = 4)") +
  geom_jitter()

ggplotly(VDT_cluster)
# Add cluster assignments to the original dataset
VDT_anova_grip_diff_RT_MLM$cluster <- as.factor(clusters)

# Calculate cluster centroids
cluster_centroids <- aggregate(cbind(RT, auc_rel_mvc) ~ cluster, data = VDT_anova_grip_diff_RT_MLM, FUN = mean)

# Visualize cluster centroids
VDT_cluster_centroids <- ggplot(VDT_anova_grip_diff_RT_MLM, aes(x = RT, y = auc_rel_mvc, color = factor(cluster))) +
  geom_point(alpha = 0.5) +
  geom_point(data = cluster_centroids, aes(x = RT, y = auc_rel_mvc, color = factor(cluster)), size = 4, shape = 18) +
  labs(x = "RT", y = "auc_rel_mvc", color = "Cluster") +
  ggtitle("Cluster Centroids — VDT") +
  geom_jitter()

ggplotly(VDT_cluster_centroids)
# Calculate within-cluster sum of squares
wss <- sum(kmeans_model$withinss)

# Print the total within-cluster sum of squares
print(paste("Total within-cluster sum of squares:", wss))
## [1] "Total within-cluster sum of squares: 2051.02190881132"
# Explore the relationship between clusters and stimulus levels
cluster_stimulus <- table(VDT_anova_grip_diff_RT_MLM$cluster, VDT_anova_grip_diff_RT_MLM$stimLev)
print(cluster_stimulus)
##    
##        1    2    3    4
##   1  270  469   50    0
##   2 1549 1347    0    0
##   3    0    0    0 1835
##   4    0    0 1775    0
# Perform association rule mining
library(arules)
data <- VDT_anova_grip_diff_RT_MLM[, c("RT", "auc_rel_mvc")]
transactions <- as(data, "transactions")
rules <- apriori(transactions, parameter = list(supp = 0.1, conf = 0.8))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.8    0.1    1 none FALSE            TRUE       5     0.1      1
##  maxlen target  ext
##      10  rules TRUE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 729 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[6 item(s), 7295 transaction(s)] done [0.00s].
## sorting and recoding items ... [6 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 done [0.00s].
## writing ... [0 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
# Print the discovered rules
inspect(rules)

6.10.4.3 CDT

CDT_anova_grip_diff_RT_MLM <- CDT_4T %>% group_by(stimLev, auc_rel_mvc, sub) %>%
  filter(stimLev != '0') %>% filter(!is.na(resp1RT)) %>%
  summarise(RT_sub = resp1RT, stimLev, auc_rel_mvc, sub) %>%
  ungroup() %>% group_by(stimLev) %>%
  summarise(RT = RT_sub, stimLev, auc_rel_mvc, sub) %>%
  distinct()
CDT_anova_grip_diff_RT_MLM$stimLev <- as.factor(CDT_anova_grip_diff_RT_MLM$stimLev)
CDT_anova_grip_diff_RT_MLM$stimLev <- recode_factor(CDT_anova_grip_diff_RT_MLM$stimLev,'5'="1", '20'="2", '45'="3", '90'="4")
CDT_anova_grip_diff_RT_MLM$task <- "CDT"


model7 <- lmer(RT ~ auc_rel_mvc * stimLev + (1 | sub), data = CDT_anova_grip_diff_RT_MLM)
summary(model7)
## Linear mixed model fit by REML ['lmerMod']
## Formula: RT ~ auc_rel_mvc * stimLev + (1 | sub)
##    Data: CDT_anova_grip_diff_RT_MLM
## 
## REML criterion at convergence: 6664.3
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.9969 -0.6237 -0.1388  0.5118  4.0471 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  sub      (Intercept) 0.03588  0.1894  
##  Residual             0.22140  0.4705  
## Number of obs: 4907, groups:  sub, 36
## 
## Fixed effects:
##                      Estimate Std. Error t value
## (Intercept)           1.47947    0.04032  36.693
## auc_rel_mvc          -0.01987    0.13547  -0.147
## stimLev2              0.09713    0.03560   2.729
## stimLev3              0.03399    0.03530   0.963
## stimLev4             -0.01329    0.03524  -0.377
## auc_rel_mvc:stimLev2 -0.10445    0.19210  -0.544
## auc_rel_mvc:stimLev3  0.17366    0.19091   0.910
## auc_rel_mvc:stimLev4  0.00237    0.18973   0.012
## 
## Correlation of Fixed Effects:
##             (Intr) ac_rl_ stmLv2 stmLv3 stmLv4 a__:L2 a__:L3
## auc_rel_mvc -0.525                                          
## stimLev2    -0.437  0.593                                   
## stimLev3    -0.440  0.598  0.499                            
## stimLev4    -0.441  0.598  0.500  0.504                     
## ac_rl_mv:L2  0.369 -0.702 -0.845 -0.422 -0.423              
## ac_rl_mv:L3  0.371 -0.707 -0.421 -0.843 -0.425  0.499       
## ac_rl_mv:L4  0.373 -0.711 -0.423 -0.427 -0.843  0.502  0.505
report(model7)
## We fitted a linear mixed model (estimated using REML and nloptwrap optimizer)
## to predict RT with auc_rel_mvc and stimLev (formula: RT ~ auc_rel_mvc *
## stimLev). The model included sub as random effect (formula: ~1 | sub). The
## model's total explanatory power is moderate (conditional R2 = 0.14) and the
## part related to the fixed effects alone (marginal R2) is of 6.42e-03. The
## model's intercept, corresponding to auc_rel_mvc = 0 and stimLev = 1, is at 1.48
## (95% CI [1.40, 1.56], t(4897) = 36.69, p < .001). Within this model:
## 
##   - The effect of auc rel mvc is statistically non-significant and negative (beta
## = -0.02, 95% CI [-0.29, 0.25], t(4897) = -0.15, p = 0.883; Std. beta =
## -3.91e-03, 95% CI [-0.06, 0.05])
##   - The effect of stimLev [2] is statistically significant and positive (beta =
## 0.10, 95% CI [0.03, 0.17], t(4897) = 2.73, p = 0.006; Std. beta = 0.16, 95% CI
## [0.09, 0.23])
##   - The effect of stimLev [3] is statistically non-significant and positive (beta
## = 0.03, 95% CI [-0.04, 0.10], t(4897) = 0.96, p = 0.336; Std. beta = 0.12, 95%
## CI [0.05, 0.19])
##   - The effect of stimLev [4] is statistically non-significant and negative (beta
## = -0.01, 95% CI [-0.08, 0.06], t(4897) = -0.38, p = 0.706; Std. beta = -0.03,
## 95% CI [-0.10, 0.05])
##   - The effect of auc rel mvc × stimLev [2] is statistically non-significant and
## negative (beta = -0.10, 95% CI [-0.48, 0.27], t(4897) = -0.54, p = 0.587; Std.
## beta = -0.02, 95% CI [-0.09, 0.05])
##   - The effect of auc rel mvc × stimLev [3] is statistically non-significant and
## positive (beta = 0.17, 95% CI [-0.20, 0.55], t(4897) = 0.91, p = 0.363; Std.
## beta = 0.03, 95% CI [-0.04, 0.11])
##   - The effect of auc rel mvc × stimLev [4] is statistically non-significant and
## positive (beta = 2.37e-03, 95% CI [-0.37, 0.37], t(4897) = 0.01, p = 0.990;
## Std. beta = 4.66e-04, 95% CI [-0.07, 0.07])
## 
## Standardized parameters were obtained by fitting the model on a standardized
## version of the dataset. 95% Confidence Intervals (CIs) and p-values were
## computed using a Wald t-distribution approximation.
# Interaction plot
interaction_plot7 <- ggplot(CDT_anova_grip_diff_RT_MLM, aes(x = auc_rel_mvc, y = RT, color = factor(stimLev))) +
  geom_line(aes(group = factor(stimLev)), size = 1) +
  geom_point(size = 3) +
  labs(x = "auc_rel_mvc", y = "RT", color = "stimLev") +
  theme_minimal()

# Display the plot
interaction_plot7

# Load necessary libraries
library(sjPlot)

# Marginal effects plot
marginal_plot7 <- plot_model(model7, type = "eff", terms = c("auc_rel_mvc", "stimLev"))

# Display the plot
print(marginal_plot7)

#alltask_anova_grip_diff_RT_MLM <- rbind(ADT_anova_grip_diff_RT_MLM,VDT_anova_grip_diff_RT_MLM,CDT_anova_grip_diff_RT_MLM)
#alltask_anova_grip_diff_RT_MLM$auc_rel_mvc <- as.factor(alltask_anova_grip_diff_RT_MLM$auc_rel_mvc)
#alltask_anova_grip_diff_RT_MLM$task <- as.factor(alltask_anova_grip_diff_RT_MLM$task)